3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Various methods of computing Fibonacci numbers in PHP */ class Fibonacci { /** * @var array Memoization cache * @see Fibonacci::memoized */ protected $cache = array(0 => 0, 1 => 1); /** * Fibonacci using recursion */ public function recursive($n) { if ($n == 0) { return 0; } if ($n == 1) { return 1; } return $this->recursive($n - 1) + $this->recursive($n - 2); } /** * Fibonacci using an iterative approach */ public function iterative($n) { $a = 0; $b = 1; for ($i = 0; $i < $n; $i++) { $c = $a; $a = $b; $b += $c; } return $a; } /** * Fibonacci using Binet's formula * @link http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html */ public function binet($n) { $phi = (1 + sqrt(5)) / 2; return (pow($phi, $n) - pow(1 - $phi, $n)) / sqrt(5); } /** * Fibonacci using a cache */ public function memoized($n) { if (!isset($this->cache[$n])) { $this->cache[$n] = $this->memoized($n - 1) + $this->memoized($n - 2); } return $this->cache[$n]; } } /** * Test each Fibonacci method and output the speed */ function test($total, $callback) { echo $callback[1] . ":\n"; $t = microtime(true); for ($x = 0; $x < $total; $x++) { call_user_func($callback, $x); } $finish = microtime(true) - $t; echo ($finish * 1000) . ' ms (' . (($finish / $total) * 1000) . ")\n\n"; } // You can pass in the total number of sequences to calculate as a CLI argument $total = isset($argv[1]) ? $argv[1] : 25; $fib = new Fibonacci(); test($total, array($fib, 'iterative')); test($total, array($fib, 'binet')); test($total, array($fib, 'memoized')); // Limit our attempts with recursion if ($total <= 30) { test($total, array($fib, 'recursive')); }

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)
7.3.120.0070.03015.41
7.3.110.0070.02815.35
7.3.100.0060.02415.13
7.3.90.0100.02615.16
7.3.80.0060.02615.20
7.3.70.0000.03615.08
7.3.60.0030.03414.78
7.3.50.0100.02415.18
7.3.40.0030.02415.05
7.3.30.0070.02615.12
7.3.20.0030.02816.95
7.3.10.0080.02516.77
7.3.00.0060.02716.77
7.2.240.0100.02415.47
7.2.230.0000.03515.34
7.2.220.0100.02315.27
7.2.210.0040.03115.45
7.2.200.0040.02515.22
7.2.190.0030.03015.43
7.2.180.0000.03815.22
7.2.170.0030.03015.45
7.2.160.0000.03215.11
7.2.150.0100.02417.22
7.2.140.0030.03317.16
7.2.130.0140.02517.07
7.2.120.0110.02917.00
7.2.110.0070.02917.00
7.2.100.0090.02816.83
7.2.90.0070.02717.03
7.2.80.0080.03116.93
7.2.70.0100.02817.03
7.2.60.0080.02816.96
7.2.50.0120.02917.12
7.2.40.0080.03417.07
7.2.30.0080.02816.98
7.2.20.0050.03116.95
7.2.10.0090.03217.25
7.2.00.0060.02617.73
7.1.330.0100.03715.93
7.1.320.0070.04115.88
7.1.310.0130.03616.00
7.1.300.0070.03616.10
7.1.290.0000.04315.97
7.1.280.0070.03715.88
7.1.270.0030.04415.77
7.1.260.0030.04516.11
7.1.250.0110.03815.77
7.1.70.0100.02117.33
7.1.60.0030.05019.40
7.1.50.0130.04017.17
7.1.00.0030.07722.65
7.0.200.0000.02717.06
7.0.140.0030.08722.13
7.0.70.0030.10021.93
7.0.60.0100.07021.75
7.0.50.0000.10322.19
7.0.40.0070.09320.26
7.0.30.0000.10020.32
7.0.20.0100.07720.39
7.0.10.0070.09720.26
7.0.00.0100.09720.32
5.6.280.0130.14721.15
5.6.220.0070.13020.62
5.6.210.0030.14720.80
5.6.200.0000.15321.34
5.6.190.0030.16021.29
5.6.180.0030.13021.19
5.6.170.0070.14321.19
5.6.160.0100.13721.34
5.6.150.0100.11721.35
5.6.140.0030.14721.34
5.6.130.0070.12721.36
5.6.120.0070.13021.18
5.6.110.0000.15021.23
5.6.100.0030.15021.34
5.6.90.0000.14721.23
5.6.80.0100.13720.52
5.6.70.0100.14720.65
5.6.60.0070.14720.61
5.6.50.0000.08720.67
5.6.40.0030.08320.61
5.6.30.0000.08720.62
5.6.20.0000.08720.71
5.6.10.0030.08320.64
5.6.00.0030.07020.70
5.5.360.0070.14020.58
5.5.350.0070.14020.46
5.5.340.0000.13320.91
5.5.330.0130.13321.12
5.5.320.0000.14321.11
5.5.310.0030.14721.12
5.5.300.0030.14020.93
5.5.290.0030.15020.89
5.5.280.0100.13321.16
5.5.270.0070.13721.05
5.5.260.0030.14321.02
5.5.250.0070.13320.69
5.5.240.0070.08020.45
5.5.230.0070.08720.46
5.5.220.0070.12720.54
5.5.210.0200.12720.52
5.5.200.0030.08320.26
5.5.190.0000.08720.49
5.5.180.0030.08020.21
5.5.160.0030.09720.49
5.5.150.0070.13020.51
5.5.140.0000.08320.40
5.5.130.0030.06720.27
5.5.120.0000.07720.22
5.5.110.0030.06720.38
5.5.100.0000.07320.33
5.5.90.0030.11320.14
5.5.80.0070.10020.33
5.5.70.0000.07020.39
5.5.60.0000.08320.26
5.5.50.0000.09020.39
5.5.40.0030.07020.39
5.5.30.0000.07020.39
5.5.20.0000.07020.26
5.5.10.0000.12720.20
5.5.00.0070.08720.28
5.4.450.0100.13719.73
5.4.440.0000.13719.46
5.4.430.0070.13319.39
5.4.420.0070.13719.62
5.4.410.0070.11719.42
5.4.400.0030.10319.06
5.4.390.0070.13319.12
5.4.380.0070.12319.32
5.4.370.0030.08718.96
5.4.360.0000.08319.14
5.4.350.0070.09019.40
5.4.340.0070.06319.12
5.4.320.0000.06719.16
5.4.310.0030.06719.34
5.4.300.0030.06319.30
5.4.290.0000.07018.96
5.4.280.0070.06319.38
5.4.270.0000.10719.28
5.4.260.0000.07319.41
5.4.250.0000.06719.21
5.4.240.0000.08019.24
5.4.230.0030.06319.29
5.4.220.0000.07019.24
5.4.210.0070.06019.05
5.4.200.0030.06719.28
5.4.190.0000.06719.20
5.4.180.0000.08719.19
5.4.170.0030.07719.07
5.4.160.0000.06719.32
5.4.150.0000.08719.04
5.4.140.0070.11716.68
5.4.130.0030.12316.44
5.4.120.0070.07716.44
5.4.110.0000.12316.70
5.4.100.0030.13016.56
5.4.90.0070.13316.57
5.4.80.0000.13016.62
5.4.70.0070.12016.66
5.4.60.0030.13316.59
5.4.50.0000.13016.55
5.4.40.0070.13016.53
5.4.30.0000.13316.69
5.4.20.0070.13316.58
5.4.10.0130.12716.57
5.4.00.0030.08715.99
5.3.290.0000.13714.93
5.3.280.0000.09014.85
5.3.270.0000.08714.84
5.3.260.0000.08314.87
5.3.250.0000.19314.78
5.3.240.0070.11014.80
5.3.230.0000.10714.86
5.3.220.0070.15314.82
5.3.210.0030.10314.73
5.3.200.0100.13314.82
5.3.190.0070.17314.83
5.3.180.0070.17014.81
5.3.170.0070.15314.71
5.3.160.0030.17014.83
5.3.150.0100.14714.72
5.3.140.0070.15314.75
5.3.130.0000.16014.71
5.3.120.0100.11314.74
5.3.110.0030.15714.81
5.3.100.0030.15314.38
5.3.90.0070.10014.18
5.3.80.0030.14714.16
5.3.70.0000.15314.29
5.3.60.0000.16014.26
5.3.50.0030.16014.09
5.3.40.0000.15314.20
5.3.30.0070.16314.16
5.3.20.0030.14713.82
5.3.10.0070.17013.82
5.3.00.0030.14313.85
5.2.170.0000.14012.52
5.2.160.0030.17712.52
5.2.150.0070.19012.52
5.2.140.0070.17712.52
5.2.130.0000.17012.52
5.2.120.0070.18712.52
5.2.110.0030.18012.52
5.2.100.0070.17312.52
5.2.90.0030.17712.52
5.2.80.0030.16312.52
5.2.70.0000.18312.52
5.2.60.0070.17012.52
5.2.50.0000.17712.52
5.2.40.0000.17012.52
5.2.30.0070.18712.52
5.2.20.0100.15712.52
5.2.10.0030.13312.52
5.2.00.0000.14012.52
5.1.60.0000.19012.52
5.1.50.0030.17012.52
5.1.40.0030.16012.52
5.1.30.0030.17712.52
5.1.20.0000.11712.52
5.1.10.0000.15712.52
5.1.00.0030.17012.52
5.0.50.0030.20012.52
5.0.40.0030.19712.52
5.0.30.0030.22312.52
5.0.20.0070.21312.52
5.0.10.0030.17712.52
5.0.00.0000.22012.52
4.4.90.0000.03712.52
4.4.80.0000.03712.52
4.4.70.0000.03712.52
4.4.60.0000.03012.52
4.4.50.0000.04012.52
4.4.40.0000.02712.52
4.4.30.0000.03312.52
4.4.20.0000.03312.52
4.4.10.0070.02712.52
4.4.00.0000.05312.52
4.3.110.0000.02312.52
4.3.100.0000.03712.52
4.3.90.0030.03312.52
4.3.80.0000.05012.52
4.3.70.0070.03012.52
4.3.60.0000.03312.52
4.3.50.0030.03012.52
4.3.40.0000.05012.52
4.3.30.0000.03712.52
4.3.20.0000.03312.52
4.3.10.0000.03712.52
4.3.00.0100.02712.52

preferences:
45.04 ms | 401 KiB | 5 Q