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 ($c->getCallStack() as $call) { echo '#'.$i.' '.$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; $i = 0; foreach ($c->getCallStack() as $call) { echo '#'.$i.' '.$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.0040.01418.56
8.3.50.0100.00721.21
8.3.40.0090.00618.82
8.3.30.0040.01120.48
8.3.20.0000.00820.16
8.3.10.0050.00322.09
8.3.00.0030.00620.77
8.2.180.0130.00716.85
8.2.170.0060.00919.22
8.2.160.0180.00322.96
8.2.150.0000.00824.18
8.2.140.0130.00324.66
8.2.130.0110.00726.16
8.2.120.0060.00319.50
8.2.110.0070.00320.99
8.2.100.0120.00018.09
8.2.90.0030.00517.97
8.2.80.0040.00417.97
8.2.70.0040.00417.63
8.2.60.0040.00418.05
8.2.50.0000.00818.07
8.2.40.0000.00819.88
8.2.30.0030.00618.33
8.2.20.0000.00917.81
8.2.10.0040.00417.90
8.2.00.0030.00617.76
8.1.280.0120.00325.92
8.1.270.0040.00424.66
8.1.260.0080.00026.35
8.1.250.0000.00828.09
8.1.240.0040.00720.93
8.1.230.0060.00620.83
8.1.220.0040.00417.74
8.1.210.0060.00318.85
8.1.200.0060.00317.38
8.1.190.0000.00817.64
8.1.180.0030.00619.13
8.1.170.0050.00318.63
8.1.160.0050.00222.14
8.1.150.0040.00418.60
8.1.140.0080.00017.57
8.1.130.0000.00817.91
8.1.120.0050.00217.48
8.1.110.0000.00817.49
8.1.100.0000.00817.44
8.1.90.0040.00417.44
8.1.80.0000.00717.54
8.1.70.0040.00417.47
8.1.60.0040.00417.61
8.1.50.0060.00317.61
8.1.40.0040.00417.63
8.1.30.0030.00517.64
8.1.20.0000.00817.58
8.1.10.0000.00817.65
8.1.00.0070.00417.55
8.0.300.0050.00319.98
8.0.290.0080.00017.14
8.0.280.0000.00718.53
8.0.270.0040.00417.36
8.0.260.0030.00517.01
8.0.250.0000.00717.11
8.0.240.0040.00417.11
8.0.230.0000.00817.07
8.0.220.0000.00816.93
8.0.210.0000.00716.96
8.0.200.0030.00317.07
8.0.190.0000.00917.20
8.0.180.0040.00417.11
8.0.170.0000.00817.12
8.0.160.0000.00717.07
8.0.150.0070.00016.96
8.0.140.0040.00417.00
8.0.130.0030.00313.43
8.0.120.0040.00417.00
8.0.110.0000.00817.00
8.0.100.0000.00717.05
8.0.90.0050.00216.90
8.0.80.0130.00316.98
8.0.70.0050.00216.82
8.0.60.0000.00716.88
8.0.50.0000.00717.04
8.0.30.0070.01217.15
8.0.20.0120.00917.40
8.0.10.0050.00317.20
8.0.00.0100.00816.89
7.4.330.0040.00415.00
7.4.320.0080.00016.57
7.4.300.0000.00816.61
7.4.290.0040.00416.71
7.4.280.0040.00416.59
7.4.270.0050.00216.60
7.4.260.0000.01016.52
7.4.250.0030.00616.59
7.4.240.0000.00716.61
7.4.230.0070.00016.70
7.4.220.0120.00616.70
7.4.210.0070.01016.66
7.4.200.0070.00016.79
7.4.190.0040.00416.75
7.4.160.0100.00616.62
7.4.150.0130.01017.40
7.4.140.0060.01417.86
7.4.130.0130.00516.69
7.4.120.0050.01516.74
7.4.110.0140.00416.73
7.4.100.0130.00516.46
7.4.90.0090.00916.70
7.4.80.0140.00419.39
7.4.70.0100.00716.67
7.4.60.0030.01716.65
7.4.50.0040.00016.71
7.4.40.0080.00822.77
7.4.30.0070.01016.54
7.4.00.0060.00915.00
7.3.330.0030.00313.36
7.3.320.0000.00613.47
7.3.310.0030.00316.43
7.3.300.0030.00316.58
7.3.290.0070.01016.60
7.3.280.0110.00616.52
7.3.270.0150.00317.40
7.3.260.0130.01016.60
7.3.250.0090.01116.62
7.3.240.0070.01116.47
7.3.230.0070.01716.39
7.3.210.0100.01016.64
7.3.200.0100.00719.39
7.3.190.0070.01016.62
7.3.180.0090.00616.66
7.3.170.0030.01316.61
7.3.160.0160.00016.60
7.3.120.0070.00815.07
7.3.110.0100.01314.86
7.3.100.0030.01214.88
7.3.90.0120.00414.99
7.3.80.0070.00715.11
7.3.70.0090.00614.54
7.3.60.0090.00614.87
7.3.50.0000.01115.14
7.3.40.0030.00715.11
7.3.30.0100.00614.94
7.3.20.0080.00416.77
7.3.10.0040.00916.67
7.3.00.0050.01116.61
7.2.330.0110.00716.74
7.2.320.0090.00916.59
7.2.310.0150.00616.89
7.2.300.0100.00716.84
7.2.290.0140.00316.79
7.2.250.0070.01114.84
7.2.240.0140.00715.21
7.2.230.0050.01415.20
7.2.220.0110.00715.18
7.2.210.0130.00315.00
7.2.200.0060.01115.06
7.2.190.0040.01115.31
7.2.180.0060.00314.88
7.2.170.0040.00915.03
7.2.130.0090.00616.98
7.2.120.0070.01017.11
7.2.110.0090.00916.86
7.2.100.0070.00716.96
7.2.90.0070.00316.94
7.2.80.0040.00717.05
7.2.70.0070.01017.15
7.2.60.0090.00716.78
7.2.50.0030.01316.93
7.2.40.0060.00917.00
7.2.30.0030.01217.04
7.2.20.0030.01017.07
7.2.10.0070.00716.82
7.2.00.0070.00617.98
7.1.330.0040.01615.83
7.1.320.0150.00315.68
7.1.310.0070.01015.93
7.1.300.0030.00615.82
7.1.290.0040.00815.98
7.1.280.0080.00815.90
7.1.270.0060.00915.78
7.1.260.0090.00615.90
7.1.250.0030.01015.61
7.1.200.0030.01016.00
7.1.100.0060.01317.80
7.1.70.0000.01117.20
7.1.60.0070.02019.21
7.1.50.0090.00617.11
7.1.00.0100.04722.37
7.0.200.0200.01015.06
7.0.140.0000.07721.97
7.0.100.0070.06320.07
7.0.90.0070.07719.92
7.0.80.0030.06320.00
7.0.70.0200.06020.04
7.0.60.0070.05320.00
7.0.50.0330.07020.42
7.0.40.0130.04020.03
7.0.30.0100.07020.02
7.0.20.0070.04020.08
7.0.10.0100.03720.11
7.0.00.0030.07020.10
5.6.280.0030.07321.19
5.6.250.0070.05720.72
5.6.240.0100.07720.70
5.6.230.0070.08320.66
5.6.220.0130.04020.67
5.6.210.0100.05320.81
5.6.200.0070.04721.20
5.6.190.0070.04721.10
5.6.180.0100.04021.01
5.6.170.0030.04721.00
5.6.160.0070.07321.16
5.6.150.0100.09021.14
5.6.140.0100.04721.14
5.6.130.0100.06021.09
5.6.120.0100.05321.01
5.6.110.0100.07021.11
5.6.100.0070.09021.04
5.6.90.0030.06721.11
5.6.80.0170.06720.39
5.6.70.0070.07020.48
5.6.60.0100.06720.39
5.6.50.0070.06020.50
5.6.40.0070.06020.50
5.6.30.0130.05020.56
5.6.20.0030.06020.52
5.6.10.0100.04720.52
5.6.00.0130.07020.36
5.5.380.0000.08320.61
5.5.370.0100.04020.48
5.5.360.0070.08320.45
5.5.350.0030.07020.45
5.5.340.0070.05020.99
5.5.330.0170.05320.98
5.5.320.0070.04320.98
5.5.310.0030.04320.78
5.5.300.0100.04020.70
5.5.290.0170.07020.89
5.5.280.0070.07320.93
5.5.270.0030.05020.91
5.5.260.0070.05020.96
5.5.250.0100.08320.77
5.5.240.0270.05720.29
5.5.230.0070.04020.25
5.5.220.0030.04020.32
5.5.210.0030.05720.24
5.5.200.0100.04320.15
5.5.190.0030.06020.19
5.5.180.0100.06320.27
5.5.160.0130.04720.15
5.5.150.0100.05020.24
5.5.140.0100.05720.28
5.5.130.0130.05320.34
5.5.120.0070.07320.14
5.5.110.0170.05720.24
5.5.100.0030.06320.02
5.5.90.0030.05720.19
5.5.80.0070.05720.23
5.5.70.0070.07020.03
5.5.60.0070.06320.11
5.5.50.0100.07720.16
5.5.40.0030.04720.06
5.5.30.0100.06720.17
5.5.20.0070.08020.12
5.5.10.0070.04020.04
5.5.00.0100.07020.11
5.4.450.0100.05019.17
5.4.440.0000.05719.21
5.4.430.0000.04319.23
5.4.420.0170.04719.48
5.4.410.0100.04019.25
5.4.400.0170.05319.14
5.4.390.0070.03719.14
5.4.380.0030.04318.84
5.4.370.0130.04719.03
5.4.360.0100.06718.85
5.4.350.0100.06319.05
5.4.340.0100.07019.03
5.4.320.0170.06318.88
5.4.310.0070.05719.23
5.4.300.0070.08019.12
5.4.290.0070.04718.90
5.4.280.0100.04018.90
5.4.270.0030.06019.17
5.4.260.0070.03719.04
5.4.250.0100.06719.22
5.4.240.0030.07719.12
5.4.230.0130.04719.12
5.4.220.0030.04318.84
5.4.210.0170.06719.09
5.4.200.0130.06019.00
5.4.190.0070.07018.89
5.4.180.0030.04018.89
5.4.170.0070.06019.20
5.4.160.0130.05719.18
5.4.150.0070.05319.00
5.4.140.0070.03716.32
5.4.130.0000.07316.40
5.4.120.0030.06016.46
5.4.110.0100.05016.54
5.4.100.0000.04016.39
5.4.90.0030.04016.54
5.4.80.0100.03016.50
5.4.70.0030.03716.46
5.4.60.0070.03316.30
5.4.50.0030.03716.42
5.4.40.0130.03016.50
5.4.30.0100.03016.45
5.4.20.0030.03716.53
5.4.10.0030.04016.50
5.4.00.0000.04015.93
5.3.290.0000.06714.82
5.3.280.0070.07314.63
5.3.270.0030.04714.78
5.3.260.0100.06314.71
5.3.250.0030.07314.61
5.3.240.0030.03714.82
5.3.230.0130.03314.73
5.3.220.0100.06314.66
5.3.210.0070.07314.73
5.3.200.0000.04014.75
5.3.190.0070.03714.67
5.3.180.0130.02714.62
5.3.170.0200.02314.67
5.3.160.0000.04014.66
5.3.150.0100.04014.67
5.3.140.0000.04314.65
5.3.130.0000.04014.55
5.3.120.0070.03714.64
5.3.110.0070.03014.65
5.3.100.0200.03014.21
5.3.90.0000.04314.03
5.3.80.0070.02714.23
5.3.70.0030.03714.11
5.3.60.0000.03714.07
5.3.50.0130.02714.04
5.3.40.0070.04013.95
5.3.30.0000.04714.01
5.3.20.0000.03313.86
5.3.10.0030.06313.74
5.3.00.0030.03313.64

preferences:
38.16 ms | 401 KiB | 5 Q