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 == '[') { if ($cell == 0) { $this->stack('jump'); $jump = $depth; } 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.70.0130.00918.58
8.3.60.0130.01018.43
8.3.50.0110.01121.27
8.3.40.0140.00718.84
8.3.30.0130.00320.35
8.3.20.0000.00820.33
8.3.10.0060.00322.00
8.3.00.0040.00420.48
8.2.190.0100.01016.88
8.2.180.0180.00416.63
8.2.170.0140.01022.96
8.2.160.0070.00720.66
8.2.150.0030.00524.18
8.2.140.0160.00624.66
8.2.130.0000.00826.16
8.2.120.0060.00319.77
8.2.110.0070.00322.37
8.2.100.0100.00319.57
8.2.90.0060.00319.48
8.2.80.0000.00819.34
8.2.70.0000.00917.63
8.2.60.0070.00318.05
8.2.50.0000.01218.07
8.2.40.0070.00317.91
8.2.30.0060.00518.15
8.2.20.0040.00417.81
8.2.10.0050.00317.78
8.2.00.0000.00817.77
8.1.280.0130.00725.92
8.1.270.0080.00824.66
8.1.260.0030.00526.35
8.1.250.0000.00928.09
8.1.240.0070.00323.97
8.1.230.0030.00919.09
8.1.220.0030.00617.91
8.1.210.0060.00318.77
8.1.200.0040.00717.60
8.1.190.0080.00017.66
8.1.180.0000.00919.01
8.1.170.0000.00918.63
8.1.160.0030.00522.31
8.1.150.0040.00418.72
8.1.140.0050.00317.49
8.1.130.0040.00417.89
8.1.120.0030.00517.46
8.1.110.0060.00317.56
8.1.100.0050.00317.55
8.1.90.0040.00417.44
8.1.80.0040.00417.52
8.1.70.0060.00317.43
8.1.60.0030.00717.64
8.1.50.0000.00817.65
8.1.40.0040.00417.61
8.1.30.0060.00317.73
8.1.20.0070.00317.64
8.1.10.0040.00417.68
8.1.00.0030.01017.61
8.0.300.0030.00518.77
8.0.290.0060.00317.04
8.0.280.0030.00618.60
8.0.270.0040.00417.25
8.0.260.0040.00416.83
8.0.250.0040.00416.97
8.0.240.0040.00417.07
8.0.230.0050.00316.93
8.0.220.0030.00516.99
8.0.210.0050.00316.97
8.0.200.0000.00717.05
8.0.190.0060.00316.93
8.0.180.0030.00716.98
8.0.170.0030.00716.91
8.0.160.0050.00317.03
8.0.150.0080.00016.95
8.0.140.0040.00416.92
8.0.130.0000.00713.39
8.0.120.0030.00616.86
8.0.110.0030.00516.87
8.0.100.0030.00616.98
8.0.90.0030.00616.80
8.0.80.0100.01116.91
8.0.70.0080.00017.04
8.0.60.0060.00316.89
8.0.50.0040.00416.95
8.0.30.0100.01517.16
8.0.20.0120.01417.47
8.0.10.0090.00016.91
8.0.00.0150.00816.88
7.4.330.0020.00415.00
7.4.320.0030.00316.68
7.4.300.0080.00016.73
7.4.290.0040.00416.71
7.4.280.0050.00316.70
7.4.270.0040.00416.65
7.4.260.0050.00516.59
7.4.250.0040.00416.66
7.4.240.0040.00416.55
7.4.230.0030.00516.45
7.4.220.0120.00916.62
7.4.210.0060.01416.68
7.4.200.0000.00816.39
7.4.190.0080.00016.77
7.4.160.0130.00616.59
7.4.150.0180.00617.40
7.4.140.0130.00917.86
7.4.130.0130.01216.66
7.4.120.0120.01016.69
7.4.110.0140.00616.49
7.4.100.0140.00716.55
7.4.90.0160.00316.57
7.4.80.0130.00619.39
7.4.70.0080.01216.64
7.4.60.0180.00716.56
7.4.50.0000.00516.32
7.4.40.0000.01822.77
7.4.30.0160.00816.64
7.4.00.0110.00414.84
7.3.330.0060.00013.30
7.3.320.0030.00313.54
7.3.310.0000.00916.34
7.3.300.0070.00016.38
7.3.290.0060.01016.42
7.3.280.0100.01316.48
7.3.270.0170.00317.40
7.3.260.0110.01216.71
7.3.250.0130.00816.59
7.3.240.0180.00916.43
7.3.230.0150.01216.72
7.3.210.0120.00916.35
7.3.200.0150.00619.39
7.3.190.0190.00816.71
7.3.180.0120.00616.80
7.3.170.0210.00316.53
7.3.160.0110.00816.72
7.3.120.0030.01415.00
7.3.110.0090.00614.76
7.3.100.0040.01214.92
7.3.90.0030.00715.17
7.3.80.0030.00914.90
7.3.70.0000.01414.84
7.3.60.0040.01114.96
7.3.50.0040.00714.98
7.3.40.0100.00614.96
7.3.30.0090.00614.88
7.3.20.0110.00716.74
7.3.10.0110.00316.72
7.3.00.0020.01216.62
7.2.330.0150.00616.87
7.2.320.0090.01816.68
7.2.310.0000.02516.78
7.2.300.0160.00816.58
7.2.290.0030.01716.66
7.2.240.0060.00915.37
7.2.230.0030.01315.17
7.2.220.0100.01015.43
7.2.210.0000.01515.29
7.2.200.0040.01515.25
7.2.190.0120.00915.21
7.2.180.0030.01014.97
7.2.170.0060.01315.11
7.2.160.0000.01314.96
7.2.150.0000.01116.95
7.2.140.0030.00916.91
7.2.130.0050.01116.83
7.2.120.0060.00916.93
7.2.110.0040.00916.90
7.2.100.0050.01017.02
7.2.90.0050.01116.94
7.2.80.0100.00517.10
7.2.70.0050.01116.84
7.2.60.0100.00516.90
7.2.50.0050.00917.06
7.2.40.0120.00317.00
7.2.30.0060.01017.02
7.2.20.0060.00916.91
7.2.10.0030.01116.80
7.2.00.0080.00817.61
7.1.330.0030.01315.88
7.1.320.0070.00715.82
7.1.310.0070.01015.88
7.1.300.0070.00715.86
7.1.290.0060.00915.84
7.1.280.0000.01215.85
7.1.270.0040.01115.94
7.1.260.0040.01515.84
7.1.250.0120.00515.77
7.1.240.0030.01015.68
7.1.230.0110.00415.73
7.1.220.0000.01115.91
7.1.210.0060.00615.88
7.1.200.0000.01315.95
7.1.190.0000.01415.71
7.1.180.0060.00915.94
7.1.170.0130.00315.84
7.1.160.0030.01015.80
7.1.150.0000.01415.82
7.1.140.0000.01215.50
7.1.130.0100.00515.38
7.1.120.0130.00015.50
7.1.110.0000.01315.79
7.1.100.0030.01316.73
7.1.90.0030.01015.62
7.1.80.0060.00615.71
7.1.70.0020.01316.12
7.1.60.0110.01017.34
7.1.50.0080.01325.30
7.1.40.0000.01415.48
7.1.30.0030.00615.91
7.1.20.0070.00715.61
7.1.10.0080.00415.95
7.1.00.0100.00715.79
7.0.330.0090.00615.46
7.0.320.0040.00915.59
7.0.310.0000.01015.45
7.0.300.0090.00615.38
7.0.290.0060.00915.43
7.0.280.0060.00615.27
7.0.270.0090.00315.02
7.0.260.0030.01014.98
7.0.250.0060.00615.34
7.0.240.0030.01315.10
7.0.230.0070.01015.12
7.0.220.0000.01815.17
7.0.210.0040.00715.29
7.0.200.0190.00814.82
7.0.190.0030.00715.00
7.0.180.0030.00915.35
7.0.170.0070.00815.18
7.0.160.0070.01015.54
7.0.150.0140.00015.33
7.0.140.0060.00615.22
7.0.130.0030.01315.36
7.0.120.0080.00415.70
7.0.110.0040.00415.52
7.0.100.0060.00615.33
7.0.90.0040.01115.38
7.0.80.0040.01515.50
7.0.70.0000.01715.45
7.0.60.0000.01115.73
7.0.50.0050.00515.58
7.0.40.0030.00613.75
7.0.30.0120.00313.92
7.0.20.0030.01013.57
7.0.10.0100.00613.72
7.0.00.0030.01013.67
5.6.380.0060.00914.79
5.6.370.0070.00714.58
5.6.360.0030.01314.54
5.6.350.0100.01014.23
5.6.340.0070.00814.80
5.6.330.0070.00714.71
5.6.320.0060.01314.86
5.6.310.0070.01014.59
5.6.300.0100.00714.64
5.6.290.0030.01314.82
5.6.280.0090.00914.56
5.6.270.0040.01413.87
5.6.260.0090.00414.64
5.6.250.0060.00614.37
5.6.240.0000.01714.33
5.6.230.0030.01314.42
5.6.220.0000.01714.34
5.6.210.0030.01414.64
5.6.200.0060.00914.64
5.6.190.0040.01214.38
5.6.180.0090.00614.80
5.6.170.0140.00714.52
5.6.160.0080.00414.53
5.6.150.0100.00714.51
5.6.140.0060.01014.86
5.6.130.0000.01814.39
5.6.120.0100.00714.52
5.6.110.0060.01314.63
5.6.100.0030.01214.69
5.6.90.0030.01214.66
5.6.80.0040.01214.61
5.6.70.0030.01314.42
5.6.60.0140.00314.71
5.6.50.0060.01214.46
5.6.40.0030.01414.73
5.6.30.0090.00614.43
5.6.20.0060.00914.64
5.6.10.0120.00614.71
5.6.00.0070.00714.73
5.5.380.0060.01611.30
5.5.370.0060.00911.33
5.5.360.0000.01411.20
5.5.350.0060.00911.23
5.5.340.0000.00911.39
5.5.330.0000.01311.49
5.5.320.0000.01911.68
5.5.310.0030.01211.50
5.5.300.0000.01311.68
5.5.290.0070.00711.39
5.5.280.0060.00911.34
5.5.270.0070.00711.30
5.5.260.0030.01011.44
5.5.250.0070.01111.36
5.5.240.0000.01811.18
5.5.230.0060.00611.34
5.5.220.0130.00011.25
5.5.210.0030.01011.43
5.5.200.0000.01711.23
5.5.190.0000.01211.25
5.5.180.0070.01011.33
5.5.170.0070.01011.34
5.5.160.0070.00711.57
5.5.150.0060.00911.34
5.5.140.0000.01511.36
5.5.130.0030.00911.18
5.5.120.0040.01111.26
5.5.110.0060.00911.18
5.5.100.0040.01411.21
5.5.90.0120.00611.52
5.5.80.0000.01111.36
5.5.70.0100.00711.18
5.5.60.0000.01611.36
5.5.50.0110.00711.42
5.5.40.0060.00911.43
5.5.30.0030.00911.45
5.5.20.0100.01011.18
5.5.10.0060.01211.39
5.5.00.0000.01411.37
5.4.450.0090.00911.18
5.4.440.0090.00911.52
5.4.430.0130.00811.25
5.4.420.0090.01111.18
5.4.410.0070.01411.39
5.4.400.0140.00811.18
5.4.390.0100.01011.37
5.4.380.0090.00611.18
5.4.370.0070.01011.25
5.4.360.0030.01511.23
5.4.350.0000.01811.18
5.4.340.0090.01211.18
5.4.330.0090.00911.18
5.4.320.0070.01011.18
5.4.310.0040.01611.28
5.4.300.0080.01211.18
5.4.290.0070.01011.24
5.4.280.0150.00811.27
5.4.270.0120.00811.32
5.4.260.0080.01611.18
5.4.250.0110.04315.02
5.4.240.0130.03315.12
5.4.230.0090.03615.08
5.4.220.0090.04615.08
5.4.210.0100.03714.99
5.4.200.0060.03815.08
5.4.190.0140.03115.11
5.4.180.0130.02915.08
5.4.170.0100.03315.12
5.4.160.0110.03014.94
5.4.150.0070.03515.06
5.4.140.0060.03613.88
5.4.130.0110.02813.81
5.4.120.0090.02913.86
5.4.110.0080.03213.94
5.4.100.0160.03113.93
5.4.90.0150.02913.92
5.4.80.0050.03513.90
5.4.70.0070.03713.86
5.4.60.0100.04013.84
5.4.50.0080.03113.84
5.4.40.0130.03513.76
5.4.30.0110.03413.85
5.4.20.0040.03913.88
5.4.10.0080.04313.88
5.4.00.0060.03513.54
5.3.290.0040.01811.18
5.3.280.0160.02812.88
5.3.270.0080.03512.92
5.3.260.0050.03712.97
5.3.250.0050.03413.02
5.3.240.0100.03212.92
5.3.230.0090.03112.97
5.3.220.0150.03712.99
5.3.210.0130.04312.95
5.3.200.0120.04212.89
5.3.190.0120.03212.96
5.3.180.0090.03212.82
5.3.170.0140.02812.95
5.3.160.0080.03112.90
5.3.150.0040.04012.91
5.3.140.0140.03012.89
5.3.130.0100.03112.94
5.3.120.0040.03713.01
5.3.110.0050.03812.82
5.3.100.0110.03112.64
5.3.90.0080.04312.68
5.3.80.0110.03612.55
5.3.70.0060.03212.74
5.3.60.0150.02812.58
5.3.50.0050.03512.59
5.3.40.0050.04112.59
5.3.30.0130.03912.62
5.3.20.0090.03512.48
5.3.10.0050.04512.53
5.3.00.0130.03312.45
5.2.170.0080.03511.25
5.2.160.0070.03011.24
5.2.150.0080.03211.20
5.2.140.0100.03811.18
5.2.130.0070.02811.22
5.2.120.0090.02611.27
5.2.110.0080.02711.16
5.2.100.0060.03911.22
5.2.90.0180.02011.26
5.2.80.0060.03911.21
5.2.70.0040.03611.21
5.2.60.0080.02911.07
5.2.50.0050.02711.10
5.2.40.0090.02611.09
5.2.30.0060.03811.21
5.2.20.0030.03311.14
5.2.10.0060.02511.04
5.2.00.0120.03010.98
5.1.60.0060.02810.63
5.1.50.0000.03110.66
5.1.40.0050.02410.62
5.1.30.0080.02610.84
5.1.20.0150.02110.85
5.1.10.0070.02610.66
5.1.00.0110.01910.70
5.0.50.0050.02410.03
5.0.40.0080.0189.88
5.0.30.0030.0349.85
5.0.20.0070.0259.78
5.0.10.0040.01211.18
5.0.00.0040.01211.18
4.4.90.0060.00011.18
4.4.80.0030.00311.18
4.4.70.0060.00311.18
4.4.60.0030.00311.18
4.4.50.0000.00911.18
4.4.40.0060.00011.18
4.4.30.0030.00311.18
4.4.20.0000.00711.18
4.4.10.0000.00811.18
4.4.00.0030.00311.18
4.3.110.0030.00311.18
4.3.100.0030.00311.18
4.3.90.0040.00211.18
4.3.80.0000.00611.18
4.3.70.0030.00311.18
4.3.60.0050.00011.18
4.3.50.0060.00311.18
4.3.40.0000.00711.18
4.3.30.0020.00211.18
4.3.20.0030.00311.18
4.3.10.0000.00911.18
4.3.00.0040.00411.18

preferences:
43.02 ms | 401 KiB | 5 Q