3v4l.org

run code in 300+ PHP versions simultaneously
<?PHP if (!class_exists('RuntimeException')) { class RuntimeException extends Exception { } } class InvalidStateException extends RuntimeException { } class Compiler { private $ptr; private $arr; private $code; private $commands = array('>', '<', '+', '-', '.', ',', '[', ']'); private $call_stack = array(); private $cellsize = 8; public function parseString($code, $throwException = true) { $this->code = $code; for ($i=0;$i<strlen($this->code);$i++) { $c = substr($this->code, $i, 1); if (!in_array($c, $this->commands)) { continue; } } } public function execute($code) { $this->arr = array(0 => 0); $this->ptr = 0; $this->code = $code; $jump = -1; $loop = array(); $depth = 0; for ($this->i=0;$this->i<strlen($this->code);$this->i++) { $c = $this->c = substr($this->code, $this->i, 1); $cell = $this->arr[$this->ptr]; if (!in_array($c, $this->commands)) { continue; } else if ($jump >= 0) { if ($c == '[') { $depth++; } if ($c == ']') { if ($depth === $jump) { $jump = -1; $this->stack(); } $depth--; } } else { if ($c == '>') { $this->increment_pointer(); } elseif ($c == '<') { $this->decrement_pointer(); } elseif ($c == '+') { $this->increment_val(); } elseif ($c == '-') { $this->decrement_val(); } elseif ($c == '.') { $this->output_val(); } elseif ($c == ',') { $this->store_val($this->input_val()); } elseif ($c == '[') { $depth++; if ($cell == 0) { $this->stack('jump'); $jump = $depth; } else { array_push($loop, $this->i); } } elseif ($c == ']') { if ($cell > 0) { $this->i = $loop[count($loop)-1]; continue; } else { array_pop($loop); $depth--; continue; } } } } } private function stack($func=null) { if (is_null($func)) { if (count($this->call_stack) == 0) { throw new InvalidStateException("Trying to pop empty call stack"); return false; } array_pop($this->call_stack); return true; } else { $info = array('command' => $this->c, 'pos' => $this->i, 'internal_function' => $func); array_push($this->call_stack, $info); echo 'Stacking '.$func.PHP_EOL; return true; } } public function minimum_cellsize() { return (-1*(pow(2, $this->cellsize-1))); } public function maximum_cellsize() { return pow(2, $this->cellsize-1)-1; } private function increment_pointer() { $this->stack(__FUNCTION__); $this->ptr++; if (!isset($this->arr[$this->ptr])) $this->arr[$this->ptr] = 0; $this->stack(); return true; } private function decrement_pointer() { $this->stack(__FUNCTION__); $this->ptr--; if ($this->ptr < 0) { throw new InvalidStateException("The data pointer is less than 0"); return false; } $this->stack(); return true; } private function increment_val() { $x = $this->arr[$this->ptr]++; if ($x < $this->minimum_cellsize()) { $this->arr[$this->ptr] = $this->maximum_cellsize(); } elseif ($x > $this->maximum_cellsize()) { $this->arr[$this->ptr] = $this->minimum_cellsize(); } return true; } private function decrement_val() { $x = $this->arr[$this->ptr]--; if ($x < $this->minimum_cellsize()) { $this->arr[$this->ptr] = $this->maximum_cellsize(); } elseif ($x > $this->maximum_cellsize()) { $this->arr[$this->ptr] = $this->minimum_cellsize(); } return true; } private function output_val() { $c = $this->arr[$this->ptr]; echo ''.chr($c); } private function store_val($chr) { $x = ord($chr); $this->arr[$this->ptr] = $x; } private function input_val() { return fgets(STDIN); } public function getCallStack() { return $this->call_stack; } public function getCells() { return $this->arr; } public function getPointer() { return $this->ptr; } } $c = new Compiler(); $bf = ' +++++++++++ >+>>>>++++++++++++++++++++++++++++++++++++++++++++ >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+> +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[- <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<< -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]] >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++ +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++ ++++++++++++++++++++++++++++++++++++++++++++.[-]<< <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-] '; try { $c->execute($bf); } catch (Exception $e) { echo PHP_EOL."An exception was thrown whilst processing your BF program.".PHP_EOL; echo $e->getMessage().":".PHP_EOL; } echo "The pointer was set to ".$c->getPointer()." and the cell array resembled:".PHP_EOL; echo "[".implode(", ", $c->getCells())."]".PHP_EOL; echo "The current stack trace was:".PHP_EOL; foreach ($c->getCallStack() as $k => $call) { echo '#'.$k.' '.$call['command'].' ('.$call['internal_function'],') on pos '.$call['pos'].PHP_EOL; } echo PHP_EOL.'Finished.'.PHP_EOL; ?>

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.40.0110.00718.83
8.3.30.0070.01420.21
8.3.20.0030.00720.19
8.3.10.0030.00722.00
8.3.00.0120.00020.82
8.2.170.0100.01022.96
8.2.160.0100.01020.42
8.2.150.0060.00324.18
8.2.140.0100.00324.66
8.2.130.0180.00426.16
8.2.120.0030.00719.66
8.2.110.0220.00022.25
8.2.100.0040.02718.03
8.2.90.0030.00619.47
8.2.80.0040.00919.41
8.2.70.0090.00417.63
8.2.60.0130.00018.18
8.2.50.0040.00718.07
8.2.40.0040.00718.16
8.2.30.0130.00318.32
8.2.20.0040.00717.82
8.2.10.0030.00917.96
8.2.00.0090.00017.91
8.1.270.0080.00323.99
8.1.260.0100.00026.35
8.1.250.0050.00528.09
8.1.240.0100.00323.97
8.1.230.0180.00519.10
8.1.220.0120.00617.74
8.1.210.0100.00318.77
8.1.200.0170.00317.48
8.1.190.0060.00617.78
8.1.180.0120.00019.14
8.1.170.0070.01018.59
8.1.160.0000.00922.25
8.1.150.0060.00318.74
8.1.140.0040.00817.48
8.1.130.0040.00717.98
8.1.120.0070.00317.52
8.1.110.0030.00917.58
8.1.100.0050.01017.65
8.1.90.0030.00617.59
8.1.80.0040.00417.68
8.1.70.0030.00617.54
8.1.60.0070.00417.77
8.1.50.0070.00417.74
8.1.40.0120.00617.59
8.1.30.0030.00717.80
8.1.20.0070.01317.61
8.1.10.0090.00917.63
8.1.00.0030.01017.63
8.0.300.0030.00618.77
8.0.290.0030.00717.18
8.0.280.0030.00718.51
8.0.270.0060.00317.21
8.0.260.0060.00316.75
8.0.250.0060.00916.96
8.0.240.0060.00616.89
8.0.230.0060.00316.98
8.0.220.0030.00616.97
8.0.210.0000.00816.82
8.0.200.0090.00017.03
8.0.190.0040.00716.91
8.0.180.0050.01016.85
8.0.170.0050.00516.95
8.0.160.0090.01216.91
8.0.150.0120.00416.93
8.0.140.0030.00916.88
8.0.130.0000.01013.41
8.0.120.0100.00316.92
8.0.110.0030.00616.91
8.0.100.0050.00916.73
8.0.90.0030.00616.96
8.0.80.0070.01516.83
8.0.70.0070.00716.77
8.0.60.0060.01016.82
8.0.50.0040.00816.73
8.0.30.0340.02517.06
8.0.20.0190.02017.40
8.0.10.0000.01716.93
8.0.00.0370.03316.91
7.4.330.0070.00715.00
7.4.320.0030.00716.50
7.4.300.0040.00816.59
7.4.290.0040.00716.47
7.4.280.0170.00416.57
7.4.270.0030.01216.57
7.4.260.0060.00616.58
7.4.250.0110.01116.46
7.4.240.0060.00516.52
7.4.230.0040.00716.38
7.4.220.0030.02416.54
7.4.210.0210.01616.58
7.4.200.0030.00716.58
7.4.190.0160.00016.67
7.4.160.0070.01616.55
7.4.150.0290.04217.40
7.4.140.0220.02617.86
7.4.130.0130.02116.46
7.4.120.0240.02416.56
7.4.110.0590.03016.60
7.4.100.0070.02716.66
7.4.90.0100.02516.44
7.4.80.0130.01019.39
7.4.70.0100.01616.39
7.4.60.0260.02316.60
7.4.50.0130.01316.49
7.4.40.0130.00722.77
7.4.30.0090.04216.24
7.4.00.0120.00914.90
7.3.330.0120.00613.48
7.3.320.0030.00513.33
7.3.310.0090.00616.17
7.3.300.0060.00616.24
7.3.290.0150.04716.40
7.3.280.0290.02116.38
7.3.270.0240.02917.40
7.3.260.0460.02816.43
7.3.250.0450.02016.43
7.3.240.0390.03916.69
7.3.230.0630.06316.46
7.3.210.0070.02316.55
7.3.200.0130.01319.39
7.3.190.0370.04216.52
7.3.180.0140.01016.62
7.3.170.0100.01416.43
7.3.160.0410.02316.55
7.3.120.0160.01315.21
7.2.330.0360.02716.65
7.2.320.0570.06416.84
7.2.310.0460.02216.75
7.2.300.0220.03216.55
7.2.290.0150.01816.82
7.2.60.0070.00716.72
7.2.50.0030.01416.97
7.2.00.0070.01019.19
7.1.200.0280.01015.76
7.1.100.0070.00717.75
7.1.70.0000.01017.01
7.1.60.0070.02119.21
7.1.50.0110.00717.11
7.0.200.0280.00414.80

preferences:
40.62 ms | 400 KiB | 5 Q