3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); namespace AzJezz\BrainFuck; use ArrayIterator; use Iterator; use RuntimeException; final class MemoryCell { public $value = 0; public function increase(): void { ++$this->value; if (256 === $this->value) { $this->value = 0; } } public function decrease(): void { --$this->value; if (-1 === $this->value) { $this->value = 255; } } } final class MemoryTable { private $position = 0; private $cells = []; public function current(): MemoryCell { $this->cells[$this->position] = $this->cells[$this->position] ?? new MemoryCell(); return $this->cells[$this->position]; } public function right(): void { ++$this->position; } public function left(): void { --$this->position; } public function free(): void { $this->position = 0; $this->cells = []; } } final class Token { public const Right = 0; public const Left = 2; public const Read = 4; public const Write = 8; public const Increase = 16; public const Decrease = 32; public const LoopIn = 64; public const LoopOut = 128; private const tokens = [ 'right' => Token::Right, // > 'left' => Token::Left, // < 'read' => Token::Read, // . 'write' => Token::Write, // , 'increase' => Token::Increase, // + 'decrease' => Token::Decrease, // - 'loop-in' => Token::LoopIn, // [ 'loop-out' => Token::LoopOut, // ] ]; private $token; public function __construct(string $token) { $this->token = static::tokens[$token]; } public function kind(): int { return $this->token; } } final class Lexer { public function tokenize(string $code): Iterator { foreach (str_split($code) as $i => $chr) { switch ($chr) { case '>': yield $i => new Token('right'); break; case '<': yield $i => new Token('left'); break; case '.': yield $i => new Token('write'); break; case ',': yield $i => new Token('read'); break; case '+': yield $i => new Token('increase'); break; case '-': yield $i => new Token('decrease'); break; case '[': yield $i => new Token('loop-in'); break; case ']': yield $i => new Token('loop-out'); break; } } } } final class Interpreter { private $lexer; private $table; private $stdin; private $stdout; /** * Interpreter constructor. * * @param resource $stdin * @param resource $stdout */ public function __construct(Lexer $lexer, $stdin, $stdout) { $this->lexer = $lexer; $this->stdin = $stdin; $this->stdout = $stdout; $this->table = new MemoryTable(); } public function run(string $code): void { $this->table->free(); $tokens = $this->lexer->tokenize($code); $this->process($tokens); } public function read(): void { do { $this->table->current()->value = ord(fread($this->stdin, 1)); } while ($this->table->current()->value > 255); } public function write(): void { fwrite($this->stdout, chr($this->table->current()->value)); } private function process(Iterator $tokens): void { while ($tokens->valid()) { /** * @var Token */ $token = $tokens->current(); if (Token::LoopIn === $token->kind()) { $inner = []; $tokens->next(); $nest = 1; while ($tokens->valid()) { $current = $tokens->current(); $inner[$tokens->key()] = $current; if (Token::LoopOut === $current->kind()) { --$nest; } elseif (Token::LoopIn === $current->kind()) { ++$nest; } $tokens->next(); if (0 === $nest) { break; } } assert(0 === $nest); array_pop($inner); while (0 !== $this->table->current()->value) { $this->process(new ArrayIterator($inner)); } continue; } if (Token::LoopOut === $token->kind()) { throw new RuntimeException('Unexpected "]" token.'); } $this->operate($token); $tokens->next(); } } private function operate(Token $token): void { switch ($token->kind()) { case Token::Right: $this->table->right(); break; case Token::Left: $this->table->left(); break; case Token::Increase: $this->table->current()->increase(); break; case Token::Decrease: $this->table->current()->decrease(); break; case Token::Read: $this->read(); break; case Token::Write: $this->write(); break; } } } $interpreter = new Interpreter(new Lexer(), STDIN, STDOUT); $interpreter->run('+[-[<<[+[--->]-[<<<]]]>>>-]>-.---.>..>.<<<<-.<+.>>>>>.>.<<.<-.'); // hello world

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0130.01318.55
8.3.50.0080.01216.30
8.3.40.0110.01119.01
8.3.30.0040.01818.79
8.3.20.0070.00321.73
8.3.10.0030.00721.73
8.3.00.0070.00220.67
8.2.180.0170.00716.49
8.2.170.0090.00922.96
8.2.160.0150.00819.58
8.2.150.0070.00324.18
8.2.140.0030.00724.66
8.2.130.0070.00326.16
8.2.120.0070.00326.16
8.2.110.0090.00621.12
8.2.100.0100.00617.84
8.2.90.0060.00620.55
8.2.80.0130.00017.97
8.2.70.0000.01318.04
8.2.60.0040.00818.05
8.2.50.0030.00918.10
8.2.40.0040.00821.20
8.2.30.0040.00819.38
8.2.20.0060.00618.23
8.2.10.0040.00819.10
8.2.00.0000.01219.24
8.1.270.0060.00622.16
8.1.260.0070.00326.35
8.1.250.0060.00628.09
8.1.240.0100.00322.54
8.1.230.0120.00321.03
8.1.220.0060.00617.91
8.1.210.0040.00819.10
8.1.200.0080.00417.47
8.1.190.0030.01017.48
8.1.180.0070.00718.10
8.1.170.0090.00318.98
8.1.160.0000.01118.96
8.1.150.0040.00718.96
8.1.140.0000.01218.99
8.1.130.0050.00520.18
8.1.120.0030.01217.53
8.1.110.0100.00617.51
8.1.100.0030.00917.53
8.1.90.0030.00817.51
8.1.80.0060.00617.58
8.1.70.0030.01017.54
8.1.60.0070.00717.72
8.1.50.0040.00817.48
8.1.40.0040.01117.63
8.1.30.0030.01017.71
8.1.20.0000.01317.68
8.1.10.0000.01317.57
8.1.00.0030.01317.59
8.0.300.0050.00519.95
8.0.290.0030.00917.00
8.0.280.0080.00418.44
8.0.270.0090.00617.34
8.0.260.0000.01120.05
8.0.250.0080.00317.13
8.0.240.0070.00317.09
8.0.230.0000.01117.00
8.0.220.0040.00716.93
8.0.210.0070.00417.05
8.0.200.0070.00317.09
8.0.190.0030.00917.03
8.0.180.0090.00317.07
8.0.170.0070.00716.94
8.0.160.0040.00816.97
8.0.150.0000.01217.04
8.0.140.0090.00616.88
8.0.130.0070.00713.50
8.0.120.0100.00316.91
8.0.110.0030.00917.06
8.0.100.0080.00416.93
8.0.90.0030.01016.94
8.0.80.0090.01217.04
8.0.70.0000.01216.83
8.0.60.0000.01216.91
8.0.50.0080.00416.97
8.0.30.0140.01717.23
8.0.20.0150.01517.13
8.0.10.0220.02217.13
8.0.00.0110.01917.20
7.4.330.0000.00815.55
7.4.320.0040.00716.68
7.4.300.0040.00716.59
7.4.290.0030.00916.65
7.4.280.0000.01216.56
7.4.270.0000.01216.67
7.4.260.0030.00713.35
7.4.250.0000.01116.63
7.4.240.0060.00616.58
7.4.230.0000.01116.75
7.4.220.0060.00616.70
7.4.210.0080.01616.65
7.4.200.0040.00816.78
7.4.160.0180.01516.55
7.4.150.0200.01316.71
7.4.140.0180.01216.70
7.4.130.0100.01916.65
7.4.120.0080.02416.71
7.4.110.0110.02316.47
7.4.100.0170.02916.66
7.4.90.0200.02416.64
7.4.80.0190.01616.48
7.4.70.0190.01316.66
7.4.60.0170.02516.55
7.4.50.0170.01616.48
7.4.40.0260.01716.43
7.4.30.0170.01716.50
7.4.20.0280.01016.45
7.4.10.0230.01316.59
7.4.00.0090.02116.65
7.3.330.0030.00916.49
7.3.320.0030.00613.29
7.3.310.0060.00616.33
7.3.300.0030.00916.55
7.3.290.0060.00616.38
7.3.280.0090.02016.63
7.3.270.0200.01316.47
7.3.260.0120.01516.60
7.3.250.0090.01316.57
7.3.240.0190.01516.49
7.3.230.0170.02316.50
7.3.220.0240.02216.49
7.3.210.0220.02216.44
7.3.200.0150.02016.46
7.3.190.0190.01916.48
7.3.180.0240.01516.44
7.3.170.0140.02016.46
7.3.160.0190.02816.41
7.3.150.0220.02216.44
7.3.140.0230.01116.38
7.3.130.0140.01916.35
7.3.120.0200.02316.32
7.3.110.0090.01915.79
7.3.100.0060.02615.68
7.3.90.0150.01815.87
7.3.80.0150.01615.72
7.3.70.0140.01815.72
7.3.60.0140.01915.74
7.3.50.0140.02015.77
7.3.40.0180.01315.62
7.3.30.0050.02415.72
7.3.20.0150.01815.67
7.3.10.0150.01815.75
7.3.00.0160.02015.84
7.2.340.0170.03116.58
7.2.330.0200.01716.48
7.2.320.0200.01316.56
7.2.310.0190.01716.45
7.2.300.0210.02516.48
7.2.290.0160.02016.53
7.2.280.0230.01916.52
7.2.270.0160.02916.45
7.2.260.0150.02416.50
7.2.250.0190.02616.51
7.2.240.0130.02115.81
7.2.230.0130.02315.79
7.2.220.0140.02015.86
7.2.210.0070.02515.91
7.2.200.0120.02215.94
7.2.190.0160.01915.94
7.2.180.0150.01515.84
7.2.170.0140.01615.87
7.2.160.0190.01915.89
7.2.150.0110.02415.91
7.2.140.0160.01416.00
7.2.130.0150.01515.99
7.2.120.0230.01815.89
7.2.110.0190.01315.91
7.2.100.0180.01216.07
7.2.90.0140.01616.04
7.2.80.0130.01915.88
7.2.70.0160.01415.86
7.2.60.0170.01615.90
7.2.50.0120.01615.92
7.2.40.0100.02216.03
7.2.30.0170.01415.92
7.2.20.0160.01915.99
7.2.10.0120.02115.93
7.2.00.0130.02215.90
7.1.330.0170.01714.87
7.1.320.0120.01914.97
7.1.310.0140.01714.97
7.1.300.0090.02314.95
7.1.290.0170.02115.00
7.1.280.0160.01814.74
7.1.270.0170.01814.82
7.1.260.0130.02214.86
7.1.250.0130.02114.85
7.1.240.0190.02615.54
7.1.230.0190.01615.73
7.1.220.0150.02115.69
7.1.210.0190.01615.65
7.1.200.0130.02815.64
7.1.190.0110.02215.72
7.1.180.0140.02515.64
7.1.170.0170.01715.55
7.1.160.0180.01615.67
7.1.150.0180.01815.56
7.1.140.0160.02015.60
7.1.130.0170.02015.61
7.1.120.0170.03215.64
7.1.110.0170.01715.55
7.1.100.0240.02715.55
7.1.90.0100.02315.65
7.1.80.0180.01915.69
7.1.70.0260.01715.52
7.1.60.0290.01815.65
7.1.50.0150.01915.67
7.1.40.0220.01115.75
7.1.30.0160.01715.77
7.1.20.0150.02115.65
7.1.10.0200.01515.61
7.1.00.0220.01315.54

preferences:
65.26 ms | 400 KiB | 5 Q