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 = false; $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 == ']') { $depth--; if ($depth === $jump) { $jump = 0; $this->stack(); } } } 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 == '[') { if ($cell == 0) { $this->stack('jump'); $jump = true; } else { array_push($loop, $this->i); $depth++; } } 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); foreach ($this->getCallStack() as $k => $call) { echo '#'.$k.' '.$call['command'].' ('.$call['internal_function'],') on pos '.$call['pos'].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 { echo "{ "; $c->execute($bf); echo " }"; } 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.60.0160.00318.31
8.3.50.0080.00821.27
8.3.40.0130.00718.98
8.3.30.0080.00820.35
8.3.20.0130.00320.11
8.3.10.0090.00922.04
8.3.00.0060.00920.71
8.2.180.0160.00616.75
8.2.170.0070.01422.96
8.2.160.0060.00920.60
8.2.150.0130.00724.18
8.2.140.0100.01024.66
8.2.130.0140.00426.16
8.2.120.0040.00419.54
8.2.110.0090.00021.18
8.2.100.0040.00817.96
8.2.90.0030.00519.30
8.2.80.0030.00517.97
8.2.70.0000.00917.88
8.2.60.0030.00618.16
8.2.50.0000.00818.10
8.2.40.0000.00818.16
8.2.30.0000.00918.09
8.2.20.0000.00817.79
8.2.10.0000.00817.89
8.2.00.0000.00817.76
8.1.280.0120.00925.92
8.1.270.0040.00424.66
8.1.260.0030.00626.35
8.1.250.0000.00928.09
8.1.240.0030.00622.72
8.1.230.0080.00418.02
8.1.220.0090.00017.79
8.1.210.0060.00318.77
8.1.200.0030.00717.48
8.1.190.0060.00317.35
8.1.180.0040.00418.10
8.1.170.0000.00918.72
8.1.160.0040.00422.10
8.1.150.0040.00418.60
8.1.140.0000.00717.39
8.1.130.0000.00717.93
8.1.120.0000.00717.46
8.1.110.0060.00317.57
8.1.100.0040.00417.41
8.1.90.0040.00417.54
8.1.80.0050.00317.55
8.1.70.0020.00517.45
8.1.60.0060.00317.68
8.1.50.0030.00517.54
8.1.40.0040.00417.68
8.1.30.0080.00017.64
8.1.20.0030.00617.74
8.1.10.0080.00017.54
8.1.00.0060.00317.60
8.0.300.0040.00418.77
8.0.290.0040.00416.75
8.0.280.0040.00418.53
8.0.270.0000.00717.26
8.0.260.0030.00316.91
8.0.250.0070.00017.04
8.0.240.0050.00317.04
8.0.230.0050.00317.09
8.0.220.0000.00716.96
8.0.210.0030.00316.91
8.0.200.0040.00417.05
8.0.190.0030.00617.05
8.0.180.0080.00017.02
8.0.170.0090.00016.91
8.0.160.0050.00217.05
8.0.150.0070.00016.97
8.0.140.0050.00316.83
8.0.130.0030.00313.41
8.0.120.0060.00316.89
8.0.110.0080.00017.00
8.0.100.0040.00416.81
8.0.90.0080.00016.85
8.0.80.0090.00717.04
8.0.70.0030.00517.03
8.0.60.0030.00616.89
8.0.50.0040.00416.91
8.0.30.0110.00917.37
8.0.20.0070.01317.40
8.0.10.0040.00417.10
8.0.00.0120.01216.75
7.4.330.0030.00315.00
7.4.320.0030.00316.59
7.4.300.0000.00616.62
7.4.290.0000.00716.60
7.4.280.0030.00316.63
7.4.270.0040.00416.65
7.4.260.0050.00516.51
7.4.250.0040.00416.42
7.4.240.0020.00516.65
7.4.230.0040.00416.70
7.4.220.0130.00916.70
7.4.210.0090.01016.66
7.4.200.0040.00416.73
7.4.190.0030.00616.56
7.4.160.0140.00316.45
7.4.150.0040.01517.40
7.4.140.0070.01617.86
7.4.130.0110.00816.61
7.4.120.0150.00616.65
7.4.110.0120.00616.71
7.4.100.0030.01616.57
7.4.90.0120.00616.50
7.4.80.0110.01319.39
7.4.70.0030.01316.61
7.4.60.0070.01116.41
7.4.50.0000.00816.64
7.4.40.0030.01322.77
7.4.30.0070.01116.67
7.4.00.0110.00815.16
7.3.330.0000.00513.45
7.3.320.0000.00513.33
7.3.310.0000.00716.49
7.3.300.0000.01016.47
7.3.290.0140.00016.41
7.3.280.0100.01016.47
7.3.270.0110.00717.40
7.3.260.0140.00416.48
7.3.250.0110.01016.67
7.3.240.0090.00916.46
7.3.230.0080.01116.66
7.3.210.0130.00716.59
7.3.200.0090.00919.39
7.3.190.0100.00716.48
7.3.180.0070.01016.38
7.3.170.0110.00516.54
7.3.160.0090.00916.40
7.3.120.0080.00815.00
7.3.110.0060.01314.82
7.3.100.0000.01514.68
7.3.90.0040.01114.90
7.3.80.0040.01514.80
7.3.70.0070.00714.89
7.3.60.0040.01115.09
7.3.50.0060.00914.99
7.3.40.0040.01114.95
7.3.30.0070.00714.76
7.3.20.0060.01016.70
7.3.10.0040.00716.78
7.3.00.0020.00916.53
7.2.330.0110.01116.71
7.2.320.0080.01116.80
7.2.310.0110.00716.88
7.2.300.0080.00816.79
7.2.290.0080.00816.81
7.2.250.0080.00815.27
7.2.240.0070.01014.83
7.2.230.0080.00815.07
7.2.220.0030.01314.99
7.2.210.0100.00715.20
7.2.200.0030.01015.22
7.2.190.0060.01014.87
7.2.180.0100.00715.01
7.2.170.0060.00315.24
7.2.130.0120.00316.96
7.2.120.0000.01117.07
7.2.110.0040.00817.09
7.2.100.0040.00817.03
7.2.90.0050.00517.01
7.2.80.0030.00716.86
7.2.70.0060.00316.82
7.2.60.0040.01216.98
7.2.50.0110.00316.84
7.2.40.0140.00016.55
7.2.30.0030.01017.12
7.2.20.0030.00716.83
7.2.10.0040.01117.00
7.2.00.0070.01017.90
7.1.330.0110.00415.59
7.1.320.0040.01115.91
7.1.310.0090.00615.86
7.1.300.0030.01216.01
7.1.290.0060.01015.53
7.1.280.0000.00915.75
7.1.270.0080.00415.61
7.1.260.0120.00315.54
7.1.250.0090.00615.64
7.1.100.0000.01318.23
7.1.70.0040.00816.93
7.1.60.0090.01219.21
7.1.50.0030.01916.93
7.1.00.0030.08322.41
7.0.200.0660.00614.71
7.0.140.0000.07322.10
7.0.100.0100.06720.02
7.0.90.0030.08720.03
7.0.80.0100.08019.96
7.0.70.0300.07721.82
7.0.60.0130.08321.94
7.0.50.0030.08022.38
7.0.40.0100.05020.39
7.0.30.0070.05020.39
7.0.20.0000.09020.43
7.0.10.0070.08320.38
7.0.00.0100.05720.38
5.6.280.0070.07320.92
5.6.250.0000.06020.68
5.6.240.0170.08020.72
5.6.230.0070.05720.63
5.6.220.0100.08320.68
5.6.210.0100.08020.55
5.6.200.0100.05721.01
5.6.190.0070.08021.08
5.6.180.0100.07721.16
5.6.170.0100.05321.00
5.6.160.0070.04721.08
5.6.150.0070.08721.07
5.6.140.0100.05321.00
5.6.130.0070.08021.02
5.6.120.0070.05021.10
5.6.110.0030.09021.16
5.6.100.0070.07021.05
5.6.90.0070.07721.06
5.6.80.0100.08320.45
5.6.70.0100.06720.42
5.6.60.0030.05720.38
5.6.50.0100.08020.55
5.6.40.0070.07320.42
5.6.30.0000.05320.44
5.6.20.0170.06720.53
5.6.10.0000.07720.43
5.6.00.0100.04320.53
5.5.380.0200.07320.52
5.5.370.0070.08320.42
5.5.360.0070.04720.54
5.5.350.0070.08320.50
5.5.340.0100.08320.82
5.5.330.0200.06720.92
5.5.320.0030.07320.99
5.5.310.0070.05020.82
5.5.300.0030.05320.81
5.5.290.0030.08720.84
5.5.280.0070.08720.97
5.5.270.0070.05320.94
5.5.260.0030.06020.85
5.5.250.0070.07020.68
5.5.240.0030.07320.28
5.5.230.0100.07720.29
5.5.220.0070.06320.35
5.5.210.0000.06320.08
5.5.200.0030.08320.21
5.5.190.0100.07720.14
5.5.180.0070.08020.25
5.5.160.0100.07320.30
5.5.150.0070.06020.05
5.5.140.0130.04320.34
5.5.130.0130.07320.25
5.5.120.0070.07720.32
5.5.110.0100.07020.26
5.5.100.0100.07320.14
5.5.90.0100.07720.23
5.5.80.0100.08020.01
5.5.70.0000.09020.15
5.5.60.0030.08320.02
5.5.50.0100.03320.12
5.5.40.0030.08720.09
5.5.30.0100.07320.20
5.5.20.0030.08020.12
5.5.10.0030.04020.10
5.5.00.0130.05720.11
5.4.450.0000.05019.42
5.4.440.0030.08019.41
5.4.430.0030.08319.48
5.4.420.0030.07319.50
5.4.410.0030.04719.07
5.4.400.0100.04019.16
5.4.390.0000.04718.95
5.4.380.0000.06319.03
5.4.370.0070.07319.03
5.4.360.0030.07719.18
5.4.350.0130.03719.03
5.4.340.0030.08019.12
5.4.320.0070.07319.04
5.4.310.0200.05719.04
5.4.300.0130.05019.02
5.4.290.0000.08719.04
5.4.280.0070.07719.06
5.4.270.0070.08019.03
5.4.260.0100.06018.95
5.4.250.0000.08018.84
5.4.240.0070.08019.16
5.4.230.0000.05019.20
5.4.220.0030.04719.17
5.4.210.0130.05019.03
5.4.200.0030.08018.89
5.4.190.0100.06319.15
5.4.180.0030.06719.02
5.4.170.0030.08319.22
5.4.160.0100.07019.02
5.4.150.0000.07319.16
5.4.140.0030.08016.55
5.4.130.0100.07016.55
5.4.120.0100.07016.38
5.4.110.0030.07016.55
5.4.100.0100.04316.34
5.4.90.0030.07016.57
5.4.80.0030.08016.53
5.4.70.0000.07716.43
5.4.60.0070.07316.52
5.4.50.0000.04316.45
5.4.40.0000.05016.54
5.4.30.0030.07316.30
5.4.20.0000.04316.53
5.4.10.0070.07016.46
5.4.00.0030.06315.91
5.3.290.0000.05314.69
5.3.280.0000.08014.73
5.3.270.0070.08014.71
5.3.260.0030.04014.74
5.3.250.0070.07014.71
5.3.240.0130.06314.66
5.3.230.0070.07714.66
5.3.220.0100.07014.65
5.3.210.0100.07714.73
5.3.200.0100.07014.68
5.3.190.0100.07014.72
5.3.180.0130.07014.56
5.3.170.0000.06714.80
5.3.160.0100.07014.70
5.3.150.0130.07014.70
5.3.140.0000.08314.65
5.3.130.0030.06314.65
5.3.120.0030.06714.59
5.3.110.0070.07714.64
5.3.100.0030.08014.23
5.3.90.0030.08314.03
5.3.80.0000.07714.11
5.3.70.0070.05714.07
5.3.60.0030.08014.05
5.3.50.0070.07013.96
5.3.40.0030.05014.08
5.3.30.0070.06013.91
5.3.20.0030.07313.82
5.3.10.0030.07013.79
5.3.00.0000.08313.60

preferences:
48.95 ms | 401 KiB | 5 Q