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; for ($this->i=0;$this->i<strlen($this->code);$this->i++) { $c = $this->c = substr($this->code, $this->i, 1); if (!in_array($c, $this->commands)) { continue; } elseif ($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 == '[') { $this->loop($this->i); } } } 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 'hey'.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.40.0120.00919.01
8.3.30.0100.00320.23
8.3.20.0040.00420.38
8.3.10.0000.00822.14
8.3.00.0080.00020.71
8.2.170.0120.00622.96
8.2.160.0060.00922.13
8.2.150.0040.00424.18
8.2.140.0110.01124.66
8.2.130.0090.00026.16
8.2.120.0080.00019.76
8.2.110.0030.00720.60
8.2.100.0090.00319.52
8.2.90.0000.00919.48
8.2.80.0000.00917.97
8.2.70.0030.00517.75
8.2.60.0080.00018.15
8.2.50.0050.00318.07
8.2.40.0000.00818.27
8.2.30.0060.00318.23
8.2.20.0040.00417.88
8.2.10.0030.00517.91
8.2.00.0040.00817.90
8.1.270.0030.00624.66
8.1.260.0080.00026.35
8.1.250.0050.00328.09
8.1.240.0060.00323.83
8.1.230.0090.00317.52
8.1.220.0030.00517.91
8.1.210.0050.00518.77
8.1.200.0030.00717.36
8.1.190.0000.00817.66
8.1.180.0030.00619.01
8.1.170.0000.00918.60
8.1.160.0020.00522.10
8.1.150.0040.00418.84
8.1.140.0000.00817.65
8.1.130.0000.00817.91
8.1.120.0000.00717.53
8.1.110.0040.00417.55
8.1.100.0040.00417.44
8.1.90.0040.00417.47
8.1.80.0000.00717.45
8.1.70.0000.00717.38
8.1.60.0000.00917.68
8.1.50.0060.00317.51
8.1.40.0030.00517.70
8.1.30.0000.00817.70
8.1.20.0030.00617.65
8.1.10.0000.00817.53
8.1.00.0060.00617.64
8.0.300.0050.00318.77
8.0.290.0060.00316.88
8.0.280.0050.00218.50
8.0.270.0050.00317.35
8.0.260.0030.00316.91
8.0.250.0040.00416.96
8.0.240.0040.00416.98
8.0.230.0000.00717.00
8.0.220.0070.00016.99
8.0.210.0030.00316.82
8.0.200.0000.00717.00
8.0.190.0060.00317.09
8.0.180.0030.00716.89
8.0.170.0080.00017.00
8.0.160.0000.00716.98
8.0.150.0040.00416.80
8.0.140.0000.00816.95
8.0.130.0000.00613.43
8.0.120.0060.00316.92
8.0.110.0080.00016.90
8.0.100.0040.00416.86
8.0.90.0040.00417.03
8.0.80.0120.00617.02
8.0.70.0050.00316.87
8.0.60.0020.00516.88
8.0.50.0000.00816.80
8.0.30.0160.00517.26
8.0.20.0140.00917.40
8.0.10.0070.00016.92
8.0.00.0130.00516.78
7.4.330.0040.00315.00
7.4.320.0030.00316.70
7.4.300.0070.00016.57
7.4.290.0050.00216.60
7.4.280.0000.00716.66
7.4.270.0000.00716.64
7.4.260.0080.00016.60
7.4.250.0040.00416.64
7.4.240.0000.00716.70
7.4.230.0000.00716.60
7.4.220.0150.00416.79
7.4.210.0110.00516.63
7.4.200.0070.00016.59
7.4.190.0000.00716.81
7.4.160.0130.00316.54
7.4.150.0040.01417.40
7.4.140.0110.00817.86
7.4.130.0080.00916.52
7.4.120.0060.01216.68
7.4.110.0070.01716.47
7.4.100.0100.01416.60
7.4.90.0100.01016.46
7.4.80.0030.01319.39
7.4.70.0100.00716.72
7.4.60.0130.00316.53
7.4.50.0040.00416.38
7.4.40.0040.00822.77
7.4.30.0120.02416.56
7.4.10.0130.00714.92
7.4.00.0060.01014.97
7.3.330.0000.00613.42
7.3.320.0030.00313.28
7.3.310.0050.00216.46
7.3.300.0040.00416.48
7.3.290.0090.01216.40
7.3.280.0080.01216.46
7.3.270.0060.01117.40
7.3.260.0070.01116.43
7.3.250.0140.00316.54
7.3.240.0130.00416.52
7.3.230.0100.00716.62
7.3.210.0100.00716.57
7.3.200.0000.01819.39
7.3.190.0090.00816.46
7.3.180.0060.00916.73
7.3.170.0000.01716.51
7.3.160.0040.01616.55
7.3.130.0150.00414.89
7.3.120.0110.00715.01
7.3.110.0070.01214.88
7.3.100.0040.01315.05
7.3.90.0070.00814.81
7.3.80.0050.01014.90
7.3.70.0080.00514.96
7.3.60.0060.00814.81
7.3.50.0020.01014.86
7.3.40.0030.00814.91
7.3.30.0070.00414.90
7.3.20.0070.00816.69
7.3.10.0060.00916.61
7.3.00.0050.00916.65
7.2.330.0110.00716.70
7.2.320.0070.01016.64
7.2.310.0090.00916.98
7.2.300.0070.01416.96
7.2.290.0080.00916.86
7.2.260.0040.01515.07
7.2.250.0070.01115.07
7.2.240.0090.00915.04
7.2.230.0070.01015.20
7.2.220.0040.00815.01
7.2.210.0050.01015.20
7.2.200.0040.01015.02
7.2.190.0060.00915.16
7.2.180.0060.00815.15
7.2.170.0050.00715.06
7.2.160.0030.01315.07
7.2.150.0000.01616.90
7.2.140.0040.01117.06
7.2.130.0060.00617.07
7.2.120.0070.00716.98
7.2.110.0030.01216.68
7.2.100.0130.00316.70
7.2.90.0000.01817.14
7.2.80.0030.00717.07
7.2.70.0060.00616.83
7.2.60.0040.01116.88
7.2.50.0070.00717.11
7.2.40.0100.00717.17
7.2.30.0070.00316.95
7.2.20.0130.00617.05
7.2.10.0060.01217.01
7.2.00.0080.00718.04
7.1.330.0060.00915.81
7.1.320.0070.00715.80
7.1.310.0050.00715.90
7.1.300.0080.00615.78
7.1.290.0060.00915.88
7.1.280.0040.00715.76
7.1.270.0040.01115.74
7.1.260.0060.00915.77
7.1.250.0000.01115.88
7.1.240.0000.01015.59
7.1.230.0040.00815.62
7.1.220.0070.00715.88
7.1.210.0050.00515.84
7.1.200.0050.00815.81
7.1.190.0000.00915.86
7.1.180.0000.01315.83
7.1.170.0000.01415.59
7.1.160.0090.00016.00
7.1.150.0000.01615.98
7.1.140.0000.01615.99
7.1.130.0060.01315.82
7.1.120.0100.00615.75
7.1.110.0070.00715.88
7.1.100.0040.00917.02
7.1.90.0000.01015.51
7.1.80.0060.00615.63
7.1.70.0060.00616.54
7.1.60.0050.01317.55
7.1.50.0060.00916.45
7.1.40.0030.00615.69
7.1.30.0090.00315.55
7.1.20.0070.01015.77
7.1.10.0040.01115.95
7.1.00.0080.01819.10
7.0.330.0040.00915.22
7.0.320.0040.01115.46
7.0.310.0060.00615.51
7.0.300.0030.01015.46
7.0.290.0070.00715.56
7.0.280.0080.00815.33
7.0.270.0060.00615.61
7.0.260.0110.00315.41
7.0.250.0030.01015.54
7.0.240.0070.00415.59
7.0.230.0040.00815.34
7.0.220.0000.01415.59
7.0.210.0030.00615.54
7.0.200.0110.00515.25
7.0.190.0040.00415.31
7.0.180.0030.01015.54
7.0.170.0080.00815.44
7.0.160.0060.00315.30
7.0.150.0080.00015.21
7.0.140.0050.04018.86
7.0.130.0080.00815.51
7.0.120.0000.01515.22
7.0.110.0090.00915.52
7.0.100.0110.04417.72
7.0.90.0080.04017.63
7.0.80.0100.04017.72
7.0.70.0160.03317.82
7.0.60.0050.03017.76
7.0.50.0060.04917.83
7.0.40.0070.04516.91
7.0.30.0070.04716.97
7.0.20.0100.03916.83
7.0.10.0100.03716.82
7.0.00.0060.04816.82
5.6.400.0070.00714.46
5.6.390.0040.01114.28
5.6.380.0110.00414.43
5.6.370.0090.00614.49
5.6.360.0030.00714.51
5.6.350.0090.00414.70
5.6.340.0030.01714.77
5.6.330.0030.01014.39
5.6.320.0030.01214.42
5.6.310.0040.00814.57
5.6.300.0000.00914.86
5.6.290.0030.01014.61
5.6.280.0060.03817.82
5.6.270.0040.01114.36
5.6.260.0070.00714.62
5.6.250.0080.04117.60
5.6.240.0110.03817.55
5.6.230.0090.04417.58
5.6.220.0070.04817.58
5.6.210.0100.04317.67
5.6.200.0080.04717.87
5.6.190.0130.03317.95
5.6.180.0130.04117.90
5.6.170.0050.04517.95
5.6.160.0120.04517.76
5.6.150.0100.04117.88
5.6.140.0090.04517.83
5.6.130.0020.04717.80
5.6.120.0050.04717.75
5.6.110.0050.04317.88
5.6.100.0080.02717.78
5.6.90.0050.03217.81
5.6.80.0050.04317.38
5.6.70.0030.04117.44
5.6.60.0120.03717.51
5.6.50.0000.04717.36
5.6.40.0100.04117.46
5.6.30.0070.04417.46
5.6.20.0060.04117.59
5.6.10.0070.03517.37
5.6.00.0030.04817.46
5.5.380.0070.04717.52
5.5.370.0080.04317.52
5.5.360.0070.04617.43
5.5.350.0050.04817.37
5.5.340.0020.04517.69
5.5.330.0160.03617.56
5.5.320.0070.04717.62
5.5.310.0070.04417.83
5.5.300.0040.05217.66
5.5.290.0120.04217.73
5.5.280.0060.04617.53
5.5.270.0120.04017.78
5.5.260.0120.04317.57
5.5.250.0050.04817.69
5.5.240.0090.04117.24
5.5.230.0100.04117.37
5.5.220.0030.04017.35
5.5.210.0030.04017.24
5.5.200.0060.04417.43
5.5.190.0080.04017.16
5.5.180.0050.04617.37
5.5.170.0060.01014.55
5.5.160.0080.04017.37
5.5.150.0050.04017.22
5.5.140.0020.04617.24
5.5.130.0090.04017.33
5.5.120.0080.04217.31
5.5.110.0080.04017.24
5.5.100.0100.04317.35
5.5.90.0030.04417.11
5.5.80.0070.04217.26
5.5.70.0050.04217.17
5.5.60.0090.03717.21
5.5.50.0080.02317.39
5.5.40.0050.02017.38
5.5.30.0100.02017.31
5.5.20.0070.01917.21
5.5.10.0030.02517.06
5.5.00.0020.02917.04
5.4.450.0020.04115.57
5.4.440.0080.04015.40
5.4.430.0080.04515.45
5.4.420.0080.04015.43
5.4.410.0100.03215.46
5.4.400.0070.04515.32
5.4.390.0040.03915.37
5.4.380.0040.04415.42
5.4.370.0070.03915.40
5.4.360.0050.04215.38
5.4.350.0080.04015.24
5.4.340.0050.04215.31
5.4.330.0030.00611.59
5.4.320.0020.04315.40
5.4.310.0080.04015.40
5.4.300.0070.03915.39
5.4.290.0030.02715.25
5.4.280.0040.04215.24
5.4.270.0090.02315.32
5.4.260.0030.04015.22
5.4.250.0030.04215.31
5.4.240.0050.04115.38
5.4.230.0050.03815.36
5.4.220.0030.02615.37
5.4.210.0070.02015.23
5.4.200.0030.02215.24
5.4.190.0080.02015.36
5.4.180.0020.02215.24
5.4.170.0090.01715.37
5.4.160.0020.02215.23
5.4.150.0030.04115.32
5.4.140.0080.04114.06
5.4.130.0080.03514.00
5.4.120.0060.03814.04
5.4.110.0050.03814.06
5.4.100.0090.01514.07
5.4.90.0040.04214.04
5.4.80.0000.04314.06
5.4.70.0030.03014.05
5.4.60.0050.03914.03
5.4.50.0020.02414.05
5.4.40.0070.02314.00
5.4.30.0060.02014.02
5.4.20.0030.02113.95
5.4.10.0030.02814.06
5.4.00.0060.01813.78
5.3.290.0120.03213.16
5.3.280.0050.04113.12
5.3.270.0080.01813.17
5.3.260.0020.02313.18
5.3.250.0060.02113.20
5.3.240.0000.02713.14
5.3.230.0050.03813.17
5.3.220.0050.03513.10
5.3.210.0050.04313.10
5.3.200.0010.02613.17
5.3.190.0030.03913.14
5.3.180.0050.02213.14
5.3.170.0030.02413.17
5.3.160.0020.03213.17
5.3.150.0030.02113.08
5.3.140.0000.02713.17
5.3.130.0050.03513.13
5.3.120.0020.02413.11
5.3.110.0070.01713.07
5.3.100.0040.02412.85
5.3.90.0070.02612.84
5.3.80.0070.01912.87
5.3.70.0020.02312.84
5.3.60.0060.02012.79
5.3.50.0030.02012.77
5.3.40.0030.02112.80
5.3.30.0050.01812.84
5.3.20.0050.01912.64
5.3.10.0030.02112.60
5.3.00.0050.02012.65

preferences:
46.54 ms | 400 KiB | 5 Q