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); 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.60.0090.01318.43
8.3.50.0120.00721.14
8.3.40.0150.00018.69
8.3.30.0080.00820.35
8.3.20.0000.00820.29
8.3.10.0090.00021.92
8.3.00.0080.00020.69
8.2.180.0130.01016.75
8.2.170.0040.01122.96
8.2.160.0070.00720.52
8.2.150.0080.00024.18
8.2.140.0080.00824.66
8.2.130.0080.00026.16
8.2.120.0040.00419.54
8.2.110.0000.01022.05
8.2.100.0070.00718.09
8.2.90.0030.00618.00
8.2.80.0000.00818.77
8.2.70.0060.00317.75
8.2.60.0050.00517.75
8.2.50.0030.00717.98
8.2.40.0040.00418.34
8.2.30.0000.00818.13
8.2.20.0040.00417.69
8.2.10.0000.00817.90
8.2.00.0040.00417.84
8.1.280.0040.01525.92
8.1.270.0040.00424.66
8.1.260.0000.00826.35
8.1.250.0040.00428.09
8.1.240.0000.01024.02
8.1.230.0060.00619.23
8.1.220.0080.00018.68
8.1.210.0040.00418.77
8.1.200.0000.00817.48
8.1.190.0040.00417.48
8.1.180.0000.00818.10
8.1.170.0030.00517.62
8.1.160.0000.00818.91
8.1.150.0000.00918.68
8.1.140.0080.00017.51
8.1.130.0050.00217.84
8.1.120.0040.00417.51
8.1.110.0040.00417.38
8.1.100.0040.00417.51
8.1.90.0000.00817.58
8.1.80.0000.00717.57
8.1.70.0030.00517.57
8.1.60.0000.00817.69
8.1.50.0000.00917.53
8.1.40.0040.00417.55
8.1.30.0000.00817.66
8.1.20.0000.00817.76
8.1.10.0040.00417.67
8.1.00.0030.00817.66
8.0.300.0000.00718.77
8.0.290.0000.00816.75
8.0.280.0040.00418.55
8.0.270.0040.00417.28
8.0.260.0000.00716.96
8.0.250.0070.00017.04
8.0.240.0060.00317.01
8.0.230.0000.00717.04
8.0.220.0050.00217.01
8.0.210.0050.00316.98
8.0.200.0000.00817.02
8.0.190.0060.00316.96
8.0.180.0090.00017.02
8.0.170.0080.00017.04
8.0.160.0000.00716.88
8.0.150.0040.00416.80
8.0.140.0020.00516.83
8.0.130.0030.00313.50
8.0.120.0030.00616.95
8.0.110.0040.00416.93
8.0.100.0000.00816.95
8.0.90.0000.00816.92
8.0.80.0130.01016.97
8.0.70.0060.00316.77
8.0.60.0000.00816.93
8.0.50.0030.00516.93
8.0.30.0180.00817.17
8.0.20.0100.01017.41
8.0.10.0060.00317.12
8.0.00.0120.00516.76
7.4.330.0000.00715.00
7.4.320.0030.00316.72
7.4.300.0030.00316.68
7.4.290.0040.00416.57
7.4.280.0080.00016.56
7.4.270.0040.00416.66
7.4.260.0060.00316.61
7.4.250.0040.00416.69
7.4.240.0030.00516.67
7.4.230.0070.00016.50
7.4.220.0100.01716.69
7.4.210.0070.00816.69
7.4.200.0040.00416.72
7.4.190.0040.00416.74
7.4.160.0060.00916.45
7.4.150.0100.00717.40
7.4.140.0060.01317.86
7.4.130.0150.00916.69
7.4.120.0110.00816.71
7.4.110.0060.01216.62
7.4.100.0040.01616.50
7.4.90.0100.00716.64
7.4.80.0110.00719.39
7.4.70.0030.01416.64
7.4.60.0160.00316.62
7.4.50.0000.00616.75
7.4.40.0070.00722.77
7.4.30.0130.00716.58
7.4.10.0100.00915.13
7.4.00.0080.01115.12
7.3.330.0030.00313.50
7.3.320.0000.00613.24
7.3.310.0000.00716.29
7.3.300.0000.00716.29
7.3.290.0080.00916.41
7.3.280.0090.00916.45
7.3.270.0100.01317.40
7.3.260.0060.01816.76
7.3.250.0070.01016.72
7.3.240.0150.01216.77
7.3.230.0030.01416.61
7.3.210.0150.00416.49
7.3.200.0150.00319.39
7.3.190.0120.00916.73
7.3.180.0090.00616.41
7.3.170.0140.00316.52
7.3.160.0120.00416.57
7.3.130.0100.01114.97
7.3.120.0070.00915.03
7.3.110.0100.01214.84
7.3.100.0030.01314.78
7.3.90.0070.00615.00
7.3.80.0050.00814.97
7.3.70.0030.01214.89
7.3.60.0100.00514.95
7.3.50.0050.01015.06
7.3.40.0030.00914.88
7.3.30.0080.00614.96
7.3.20.0090.00516.69
7.3.10.0090.00516.64
7.3.00.0060.00916.68
7.2.330.0100.00716.93
7.2.320.0040.01416.60
7.2.310.0090.00916.86
7.2.300.0060.00916.95
7.2.290.0140.00416.91
7.2.260.0130.00915.00
7.2.250.0060.01414.87
7.2.240.0060.00815.34
7.2.230.0060.01115.28
7.2.220.0090.00715.33
7.2.210.0080.00615.09
7.2.200.0050.00915.10
7.2.190.0090.00815.02
7.2.180.0060.00715.34
7.2.170.0110.00314.91
7.2.160.0100.00315.09
7.2.150.0060.00516.89
7.2.140.0080.00516.95
7.2.130.0020.01116.99
7.2.120.0040.01116.60
7.2.110.0050.00916.90
7.2.100.0030.00917.13
7.2.90.0100.00317.05
7.2.80.0000.01417.03
7.2.70.0030.01116.99
7.2.60.0060.00916.99
7.2.50.0100.00717.02
7.2.40.0050.01417.03
7.2.30.0050.01016.94
7.2.20.0070.00816.98
7.2.10.0020.01216.84
7.2.00.0060.01117.67
7.1.330.0060.00915.87
7.1.320.0010.01315.79
7.1.310.0080.00515.91
7.1.300.0080.00616.03
7.1.290.0060.00815.86
7.1.280.0050.01215.86
7.1.270.0020.01015.82
7.1.260.0070.00715.77
7.1.250.0090.00615.82
7.1.240.0020.01115.88
7.1.230.0090.00815.86
7.1.220.0030.01115.70
7.1.210.0040.00915.77
7.1.200.0070.00515.69
7.1.190.0080.00615.79
7.1.180.0060.00915.66
7.1.170.0020.01015.59
7.1.160.0120.00415.85
7.1.150.0060.00815.96
7.1.140.0000.01215.89
7.1.130.0030.01015.93
7.1.120.0060.00615.69
7.1.110.0030.01315.86
7.1.100.0040.00916.52
7.1.90.0030.01215.94
7.1.80.0050.00915.93
7.1.70.0040.00816.10
7.1.60.0070.01116.83
7.1.50.0050.01016.19
7.1.40.0090.00315.70
7.1.30.0060.00815.53
7.1.20.0050.01015.91
7.1.10.0020.01115.74
7.1.00.0030.01818.03
7.0.330.0040.00715.30
7.0.320.0040.01015.44
7.0.310.0070.00515.29
7.0.300.0060.01015.34
7.0.290.0040.00915.29
7.0.280.0050.00915.46
7.0.270.0070.00715.46
7.0.260.0080.00515.45
7.0.250.0020.01215.57
7.0.240.0070.00615.46
7.0.230.0020.00815.30
7.0.220.0070.00615.28
7.0.210.0070.00915.46
7.0.200.0100.00915.36
7.0.190.0050.01115.52
7.0.180.0050.00815.42
7.0.170.0090.00415.38
7.0.160.0040.00915.49
7.0.150.0070.00715.44
7.0.140.0020.03017.61
7.0.130.0030.01015.44
7.0.120.0050.01115.31
7.0.110.0050.00715.48
7.0.100.0090.03416.90
7.0.90.0160.03416.91
7.0.80.0110.02216.98
7.0.70.0060.03217.05
7.0.60.0160.02917.37
7.0.50.0040.03217.43
7.0.40.0050.02115.99
7.0.30.0040.03415.86
7.0.20.0080.03015.95
7.0.10.0060.03015.90
7.0.00.0020.01915.88
5.6.400.0070.00814.36
5.6.390.0030.01214.69
5.6.380.0040.00914.78
5.6.370.0000.01514.45
5.6.360.0080.00614.67
5.6.350.0020.01714.44
5.6.340.0080.00814.54
5.6.330.0030.01214.32
5.6.320.0030.00914.47
5.6.310.0030.01214.39
5.6.300.0030.01014.53
5.6.290.0080.00514.58
5.6.280.0090.02816.82
5.6.270.0080.01114.59
5.6.260.0070.01314.38
5.6.250.0060.02616.52
5.6.240.0090.02716.53
5.6.230.0040.02216.68
5.6.220.0130.03116.59
5.6.210.0120.02016.64
5.6.200.0090.03916.76
5.6.190.0060.03916.63
5.6.180.0040.02816.81
5.6.170.0050.02116.72
5.6.160.0070.03316.68
5.6.150.0040.03616.72
5.6.140.0050.02316.80
5.6.130.0070.03216.73
5.6.120.0060.03316.74
5.6.110.0050.03616.90
5.6.100.0080.02516.58
5.6.90.0070.03216.68
5.6.80.0060.03016.48
5.6.70.0090.02916.53
5.6.60.0030.03316.46
5.6.50.0070.01716.56
5.6.40.0030.02616.44
5.6.30.0080.03516.48
5.6.20.0100.03016.51
5.6.10.0050.03116.51
5.6.00.0090.02116.44
5.5.380.0060.02316.57
5.5.370.0110.02816.61
5.5.360.0070.03516.63
5.5.350.0060.03416.39
5.5.340.0110.02816.54
5.5.330.0040.02516.50
5.5.320.0120.02816.60
5.5.310.0040.03216.52
5.5.300.0020.03716.55
5.5.290.0090.03116.58
5.5.280.0050.03616.60
5.5.270.0070.02916.69
5.5.260.0030.02916.74
5.5.250.0100.03316.35
5.5.240.0070.03316.25
5.5.230.0010.03916.29
5.5.220.0030.02016.39
5.5.210.0090.02716.27
5.5.200.0110.02516.28
5.5.190.0090.03316.29
5.5.180.0080.03316.30
5.5.170.0070.01014.52
5.5.160.0040.03616.26
5.5.150.0050.03316.34
5.5.140.0100.02716.32
5.5.130.0030.02416.29
5.5.120.0100.02016.34
5.5.110.0060.03316.29
5.5.100.0040.03116.23
5.5.90.0070.03016.38
5.5.80.0100.02916.45
5.5.70.0020.03616.42
5.5.60.0080.01816.28
5.5.50.0060.02716.35
5.5.40.0070.02816.30
5.5.30.0080.02916.26
5.5.20.0030.02816.28
5.5.10.0100.01716.34
5.5.00.0040.02216.32
5.4.450.0050.02913.78
5.4.440.0060.02514.05
5.4.430.0030.03214.02
5.4.420.0080.03113.92
5.4.410.0060.02913.96
5.4.400.0100.02613.80
5.4.390.0080.02013.81
5.4.380.0020.02013.88
5.4.370.0060.02713.79
5.4.360.0040.02113.64
5.4.350.0040.02413.79
5.4.340.0030.02413.81
5.4.330.0030.01011.02
5.4.320.0090.02513.68
5.4.310.0060.01813.71
5.4.300.0050.03013.81
5.4.290.0010.03213.80
5.4.280.0090.02813.85
5.4.270.0020.03113.69
5.4.260.0050.01713.78
5.4.250.0060.03213.78
5.4.240.0050.03013.79
5.4.230.0090.02713.82
5.4.220.0060.02513.82
5.4.210.0060.01913.74
5.4.200.0030.02513.83
5.4.190.0030.02013.76
5.4.180.0070.03213.69
5.4.170.0040.02913.73
5.4.160.0060.01613.66
5.4.150.0040.02013.69
5.4.140.0040.02613.05
5.4.130.0060.01713.01
5.4.120.0060.02012.86
5.4.110.0070.02012.96
5.4.100.0030.03512.85
5.4.90.0070.03013.03
5.4.80.0060.02813.10
5.4.70.0030.03012.95
5.4.60.0070.02612.96
5.4.50.0030.03212.89
5.4.40.0070.01812.99
5.4.30.0060.02813.01
5.4.20.0010.03112.79
5.4.10.0020.01912.92
5.4.00.0070.02212.71
5.3.290.0060.03312.09
5.3.280.0020.02012.09
5.3.270.0070.03111.99
5.3.260.0080.03311.96
5.3.250.0100.02012.08
5.3.240.0040.02511.97
5.3.230.0070.02912.07
5.3.220.0030.02912.02
5.3.210.0060.01911.98
5.3.200.0070.02912.03
5.3.190.0040.03312.02
5.3.180.0030.02612.12
5.3.170.0040.03512.04
5.3.160.0020.02712.10
5.3.150.0060.03212.14
5.3.140.0020.03412.07
5.3.130.0030.03212.00
5.3.120.0080.01612.01
5.3.110.0040.02912.10
5.3.100.0020.01811.89
5.3.90.0040.01811.89
5.3.80.0040.03311.86
5.3.70.0010.02211.86
5.3.60.0060.03011.88
5.3.50.0070.03011.81
5.3.40.0070.02811.83
5.3.30.0060.02811.91
5.3.20.0090.02911.75
5.3.10.0020.03011.67
5.3.00.0070.01811.61

preferences:
55.86 ms | 400 KiB | 5 Q