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); 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.0160.00318.43
8.3.50.0110.00821.14
8.3.40.0110.00418.93
8.3.30.0070.00720.20
8.3.20.0000.00820.39
8.3.10.0000.00822.05
8.3.00.0080.00020.69
8.2.180.0040.01116.63
8.2.170.0150.00622.96
8.2.160.0110.00420.40
8.2.150.0040.00424.18
8.2.140.0080.00024.66
8.2.130.0080.00026.16
8.2.120.0050.00319.67
8.2.110.0060.00322.26
8.2.100.0100.00718.03
8.2.90.0060.00319.34
8.2.80.0040.00419.59
8.2.70.0000.00817.88
8.2.60.0040.00418.15
8.2.50.0090.00018.07
8.2.40.0050.00318.34
8.2.30.0070.00418.28
8.2.20.0040.00417.88
8.2.10.0030.00517.84
8.2.00.0000.00817.84
8.1.280.0100.00725.92
8.1.270.0050.00324.66
8.1.260.0080.00026.35
8.1.250.0000.00828.09
8.1.240.0070.00323.95
8.1.230.0110.00019.13
8.1.220.0030.00617.74
8.1.210.0030.00618.77
8.1.200.0070.00317.60
8.1.190.0000.00817.79
8.1.180.0060.00319.13
8.1.170.0070.00418.46
8.1.160.0050.00322.09
8.1.150.0040.00418.77
8.1.140.0000.00817.47
8.1.130.0000.00717.92
8.1.120.0040.00417.50
8.1.110.0000.00817.51
8.1.100.0050.00317.60
8.1.90.0050.00317.54
8.1.80.0000.00817.63
8.1.70.0000.00717.46
8.1.60.0060.00317.71
8.1.50.0040.00417.63
8.1.40.0030.00617.63
8.1.30.0060.00317.72
8.1.20.0000.00917.64
8.1.10.0000.00817.63
8.1.00.0060.00317.59
8.0.300.0080.00018.77
8.0.290.0000.00817.16
8.0.280.0050.00218.60
8.0.270.0070.00016.99
8.0.260.0030.00318.51
8.0.250.0040.00416.93
8.0.240.0040.00416.94
8.0.230.0000.00717.12
8.0.220.0000.00717.01
8.0.210.0040.00416.98
8.0.200.0030.00316.97
8.0.190.0000.00817.13
8.0.180.0050.00216.93
8.0.170.0060.00317.01
8.0.160.0000.00716.98
8.0.150.0000.00717.00
8.0.140.0040.00416.85
8.0.130.0000.00613.43
8.0.120.0000.00916.87
8.0.110.0030.00516.98
8.0.100.0040.00416.79
8.0.90.0070.00016.99
8.0.80.0120.00617.03
8.0.70.0000.00817.07
8.0.60.0030.00616.82
8.0.50.0040.00416.79
8.0.30.0070.01217.18
8.0.20.0120.00717.40
8.0.10.0000.00816.98
8.0.00.0140.00917.01
7.4.330.0000.00515.09
7.4.320.0000.00716.56
7.4.300.0080.00016.61
7.4.290.0000.00716.69
7.4.280.0000.00716.59
7.4.270.0030.00316.58
7.4.260.0030.00616.57
7.4.250.0000.00816.55
7.4.240.0030.00416.67
7.4.230.0040.00416.51
7.4.220.0120.01216.54
7.4.210.0100.00916.69
7.4.200.0040.00416.46
7.4.190.0040.00416.86
7.4.160.0110.00616.56
7.4.150.0090.00917.40
7.4.140.0070.01317.86
7.4.130.0100.00716.64
7.4.120.0120.00616.59
7.4.110.0100.01016.59
7.4.100.0070.01116.63
7.4.90.0100.00716.42
7.4.80.0140.00319.39
7.4.70.0090.00916.71
7.4.60.0130.01016.59
7.4.50.0000.00816.46
7.4.40.0100.00722.77
7.4.30.0120.01216.69
7.4.00.0110.00714.93
7.3.330.0030.00313.49
7.3.320.0000.00513.47
7.3.310.0000.00716.37
7.3.300.0030.00516.41
7.3.290.0100.00316.45
7.3.280.0040.01216.49
7.3.270.0110.00617.40
7.3.260.0070.01116.30
7.3.250.0070.01016.62
7.3.240.0110.01116.52
7.3.230.0120.00816.53
7.3.210.0070.01016.46
7.3.200.0030.01719.39
7.3.190.0120.00816.61
7.3.180.0150.00416.82
7.3.170.0060.01016.77
7.3.160.0080.00916.60
7.3.120.0070.01015.04
7.3.10.0110.00316.61
7.3.00.0040.00716.71
7.2.330.0090.00916.85
7.2.320.0070.01116.71
7.2.310.0070.01016.68
7.2.300.0070.01016.82
7.2.290.0150.00316.77
7.2.130.0040.00717.01
7.2.120.0060.00816.68
7.2.110.0030.00916.80
7.2.100.0050.01016.69
7.2.90.0060.00616.79
7.2.80.0050.01016.84
7.2.70.0070.00716.81
7.2.60.0050.00916.94
7.2.50.0090.00616.91
7.2.40.0070.00817.01
7.2.30.0050.00916.91
7.2.20.0050.00817.00
7.2.10.0160.00016.87
7.2.00.0050.00917.77
7.1.250.0030.00715.76
7.1.240.0030.00915.82
7.1.230.0040.01115.93
7.1.220.0030.01015.93
7.1.210.0070.00715.85
7.1.200.0100.00015.74
7.1.190.0060.00915.76
7.1.180.0060.00615.96
7.1.170.0080.00416.05
7.1.160.0030.01015.93
7.1.150.0030.01015.84
7.1.140.0170.00015.52
7.1.130.0090.00415.73
7.1.120.0000.01115.90
7.1.110.0000.01115.91
7.1.100.0070.00516.87
7.1.90.0120.00015.81
7.1.80.0030.01015.96
7.1.70.0070.00716.38
7.1.60.0070.01417.60
7.1.50.0080.00616.41
7.1.40.0040.01115.94
7.1.30.0040.00815.80
7.1.20.0070.01015.94
7.1.10.0090.00315.93
7.1.00.0090.03019.22
7.0.320.0080.00415.44
7.0.310.0030.01015.66
7.0.300.0080.00815.52
7.0.290.0000.00815.39
7.0.280.0030.01015.59
7.0.270.0090.00315.50
7.0.260.0030.01315.20
7.0.250.0070.00715.61
7.0.240.0060.00315.55
7.0.230.0090.00615.33
7.0.220.0000.00715.29
7.0.210.0100.00315.31
7.0.200.0140.00515.10
7.0.190.0050.00515.63
7.0.180.0130.00315.57
7.0.170.0000.00815.39
7.0.160.0080.00415.11
7.0.150.0030.01015.38
7.0.140.0040.04118.68
7.0.130.0030.00615.55
7.0.120.0130.00015.36
7.0.110.0030.01015.57
7.0.100.0080.04517.82
7.0.90.0250.04317.70
7.0.80.0060.04317.68
7.0.70.0100.04317.64
7.0.60.0030.04618.02
7.0.50.0090.03718.36
7.0.40.0030.03517.10
7.0.30.0080.02717.23
7.0.20.0080.04817.29
7.0.10.0050.03017.19
7.0.00.0050.05417.08
5.6.380.0070.00714.51
5.6.370.0070.00614.30
5.6.360.0110.00714.73
5.6.350.0040.01414.36
5.6.340.0000.01414.61
5.6.330.0000.01114.65
5.6.320.0040.01114.50
5.6.310.0000.01414.48
5.6.300.0040.00814.78
5.6.290.0000.01014.87
5.6.280.0030.04217.75
5.6.270.0060.00914.36
5.6.260.0070.00414.53
5.6.250.0050.02517.63
5.6.240.0060.03317.54
5.6.230.0100.04517.68
5.6.220.0020.03517.69
5.6.210.0040.03517.58
5.6.200.0080.04517.88
5.6.190.0120.04217.82
5.6.180.0080.04317.64
5.6.170.0100.02718.00
5.6.160.0100.04317.73
5.6.150.0050.05517.80
5.6.140.0100.03117.85
5.6.130.0050.04717.75
5.6.120.0030.04817.96
5.6.110.0090.04717.73
5.6.100.0070.04517.80
5.6.90.0080.03317.99
5.6.80.0090.04217.50
5.6.70.0070.02717.43
5.6.60.0080.04517.39
5.6.50.0070.04117.56
5.6.40.0090.04417.58
5.6.30.0050.04217.48
5.6.20.0080.02617.42
5.6.10.0050.05017.37
5.6.00.0050.03017.40
5.5.380.0060.03016.04
5.5.370.0140.03215.85
5.5.360.0030.04715.87
5.5.350.0050.04215.96
5.5.340.0120.03716.01
5.5.330.0040.05116.12
5.5.320.0050.04316.00
5.5.310.0070.04316.06
5.5.300.0070.04216.19
5.5.290.0050.04516.20
5.5.280.0020.04616.12
5.5.270.0060.03016.30
5.5.260.0110.04516.08
5.5.250.0040.02415.85
5.5.240.0110.04015.87
5.5.230.0110.02315.65
5.5.220.0080.03615.82
5.5.210.0050.05015.82
5.5.200.0070.04315.80
5.5.190.0040.02915.67
5.5.180.0100.04015.88
5.5.170.0030.00914.66
5.5.160.0070.03815.69
5.5.150.0040.02715.85
5.5.140.0060.04515.72
5.5.130.0030.03315.71
5.5.120.0050.04015.76
5.5.110.0030.04715.56
5.5.100.0050.04115.75
5.5.90.0080.04115.68
5.5.80.0100.04115.70
5.5.70.0030.04615.71
5.5.60.0060.03715.82
5.5.50.0030.04615.84
5.5.40.0030.03515.79
5.5.30.0050.04115.86
5.5.20.0080.04215.86
5.5.10.0030.04715.80
5.5.00.0050.03815.59
5.4.450.0040.04615.38
5.4.440.0130.03315.41
5.4.430.0070.02715.32
5.4.420.0060.04015.38
5.4.410.0090.04115.31
5.4.400.0050.03015.21
5.4.390.0020.05015.28
5.4.380.0050.03515.09
5.4.370.0100.03715.28
5.4.360.0100.02015.20
5.4.350.0020.03415.24
5.4.340.0050.04115.28
5.4.330.0050.00511.28
5.4.320.0080.02215.11
5.4.310.0000.02515.12
5.4.300.0050.04315.19
5.4.290.0100.04015.13
5.4.280.0020.04715.23
5.4.270.0050.04815.11
5.4.260.0100.03315.11
5.4.250.0060.03915.15
5.4.240.0030.05115.07
5.4.230.0050.04315.22
5.4.220.0080.04015.12
5.4.210.0050.03915.26
5.4.200.0090.03115.12
5.4.190.0070.04815.20
5.4.180.0060.02515.20
5.4.170.0080.03715.23
5.4.160.0080.03815.12
5.4.150.0030.04214.93
5.4.140.0000.04713.89
5.4.130.0070.03813.96
5.4.120.0070.04413.88
5.4.110.0000.03713.92
5.4.100.0050.03313.86
5.4.90.0050.04313.92
5.4.80.0030.02913.79
5.4.70.0030.02013.72
5.4.60.0050.02113.88
5.4.50.0050.04013.80
5.4.40.0070.01813.93
5.4.30.0070.03513.97
5.4.20.0070.04213.79
5.4.10.0060.03913.81
5.4.00.0070.03213.59
5.3.290.0050.02812.81
5.3.280.0100.03912.66
5.3.270.0100.02312.57
5.3.260.0050.02312.71
5.3.250.0050.02812.62
5.3.240.0050.03112.70
5.3.230.0080.04012.77
5.3.220.0060.03612.64
5.3.210.0090.03112.68
5.3.200.0070.04212.71
5.3.190.0060.04012.74
5.3.180.0100.03712.78
5.3.170.0080.02112.78
5.3.160.0070.03712.62
5.3.150.0090.03912.77
5.3.140.0090.03412.68
5.3.130.0080.03912.53
5.3.120.0060.02612.70
5.3.110.0140.03712.62
5.3.100.0120.03712.46
5.3.90.0050.03712.40
5.3.80.0090.02312.22
5.3.70.0040.02412.34
5.3.60.0060.03712.32
5.3.50.0140.03912.35
5.3.40.0090.03912.26
5.3.30.0090.03712.33
5.3.20.0090.02412.16
5.3.10.0080.03711.97
5.3.00.0070.02312.09

preferences:
41.8 ms | 404 KiB | 6 Q