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.2.00.0100.01919.59
7.1.70.0070.02617.39
7.1.60.0130.04019.82
7.1.50.0100.04217.29
7.1.00.0000.10322.67
7.0.200.0030.02016.92
7.0.140.0000.09722.22
7.0.100.0030.09720.24
7.0.90.0100.10720.23
7.0.80.0070.05320.13
7.0.70.0030.05320.20
7.0.60.0000.06020.11
7.0.50.0030.05720.58
7.0.40.0030.05720.26
7.0.30.0030.05720.30
7.0.20.0030.06020.41
7.0.10.0030.05720.34
7.0.00.0130.05720.35
5.6.280.0000.15321.08
5.6.250.0000.11720.91
5.6.240.0070.10020.68
5.6.230.0000.09020.78
5.6.220.0070.09020.84
5.6.210.0030.13020.84
5.6.200.0100.10721.20
5.6.190.0030.09021.36
5.6.180.0100.11021.15
5.6.170.0000.09321.39
5.6.160.0070.12321.30
5.6.150.0030.09021.38
5.6.140.0070.08721.30
5.6.130.0130.11021.35
5.6.120.0030.10021.20
5.6.110.0030.15021.35
5.6.100.0070.14721.26
5.6.90.0030.15021.34
5.6.80.0130.12720.59
5.6.70.0230.09020.68
5.6.60.0100.11020.67
5.6.50.0130.08020.64
5.6.40.0100.11720.61
5.6.30.0100.14320.48
5.6.20.0100.11720.68
5.6.10.0070.15020.57
5.6.00.0070.13320.61
5.5.380.0030.12020.55
5.5.370.0070.08320.60
5.5.360.0030.09020.62
5.5.350.0000.09020.64
5.5.340.0030.08721.18
5.5.330.0030.13721.06
5.5.320.0000.09320.96
5.5.310.0030.10020.92
5.5.300.0100.09320.95
5.5.290.0030.11320.91
5.5.280.0070.13721.14
5.5.270.0100.13021.04
5.5.260.0100.15320.96
5.5.250.0100.14320.84
5.5.240.0100.12020.28
5.5.230.0100.13320.27
5.5.220.0000.13020.51
5.5.210.0070.16020.44
5.5.200.0170.13020.41
5.5.190.0070.11320.48
5.5.180.0070.13720.50
5.5.160.0170.12320.39
5.5.150.0200.11020.46
5.5.140.0030.13720.43
5.5.130.0070.13320.53
5.5.120.0030.13020.41
5.5.110.0100.13020.37
5.5.100.0030.14020.42
5.5.90.0030.14020.42
5.5.80.0170.12720.42
5.5.70.0070.13720.31
5.5.60.0170.12020.32
5.5.50.0030.14720.33
5.5.40.0130.13320.19
5.5.30.0030.12020.41
5.5.20.0170.12720.15
5.5.10.0030.09320.17
5.5.00.0070.14720.27
5.4.450.0000.09319.59
5.4.440.0070.14319.39
5.4.430.0030.12719.30
5.4.420.0030.14019.45
5.4.410.0100.11719.26
5.4.400.0200.11719.14
5.4.390.0070.10319.26
5.4.380.0100.10019.13
5.4.370.0030.10019.00
5.4.360.0070.13719.31
5.4.350.0130.13719.42
5.4.340.0170.13019.37
5.4.320.0030.12019.23
5.4.310.0100.12719.27
5.4.300.0070.12719.30
5.4.290.0070.10319.21
5.4.280.0170.12319.14
5.4.270.0100.13019.17
5.4.260.0070.12719.19
5.4.250.0130.07319.10
5.4.240.0070.12719.37
5.4.230.0100.13019.41
5.4.220.0100.12719.39
5.4.210.0030.13019.30
5.4.200.0070.11719.09
5.4.190.0100.12319.31
5.4.180.0100.12019.30
5.4.170.0100.12019.25
5.4.160.0070.14719.34
5.4.150.0100.11319.24
5.4.140.0100.11316.74
5.4.130.0030.11016.60
5.4.120.0100.12016.70
5.4.110.0030.14016.68
5.4.100.0070.13016.64
5.4.90.0030.08316.73
5.4.80.0070.12716.66
5.4.70.0070.10016.50
5.4.60.0030.12016.67
5.4.50.0070.13316.62
5.4.40.0100.13016.69
5.4.30.0030.14016.73
5.4.20.0130.11016.59
5.4.10.0100.12316.67
5.4.00.0100.12716.00
5.3.290.0030.11014.80
5.3.280.0070.15014.86
5.3.270.0130.15014.70
5.3.260.0130.15014.88
5.3.250.0070.16014.75
5.3.240.0030.13314.87
5.3.230.0030.16014.73
5.3.220.0100.15014.80
5.3.210.0100.11714.84
5.3.200.0070.19014.76
5.3.190.0070.12014.70
5.3.180.0130.16014.91
5.3.170.0070.10714.84
5.3.160.0130.14314.76
5.3.150.0030.14014.83
5.3.140.0030.16714.84
5.3.130.0070.16314.89
5.3.120.0030.12714.81
5.3.110.0100.17014.75
5.3.100.0130.14714.28
5.3.90.0030.17314.28
5.3.80.0100.15314.28
5.3.70.0030.17014.28
5.3.60.0170.15014.28
5.3.50.0100.14014.28
5.3.40.0070.16014.28
5.3.30.0070.16014.28
5.3.20.0030.15014.28
5.3.10.0070.14314.28
5.3.00.0030.15314.28
5.2.170.0100.17714.28
5.2.160.0070.16714.28
5.2.150.0100.17014.28
5.2.140.0070.19014.28
5.2.130.0070.16014.28
5.2.120.0030.16714.28
5.2.110.0000.17714.28
5.2.100.0000.15314.28
5.2.90.0030.17014.28
5.2.80.0030.15314.28
5.2.70.0070.18314.28
5.2.60.0070.16714.28
5.2.50.0030.13014.28
5.2.40.0070.12714.28
5.2.30.0070.16314.28
5.2.20.0100.17014.28
5.2.10.0100.13314.28
5.2.00.0030.18314.28
5.1.60.0030.18714.28
5.1.50.0030.16714.28
5.1.40.0070.10714.28
5.1.30.0030.17714.28
5.1.20.0100.15314.28
5.1.10.0030.15014.28
5.1.00.0000.14314.28
5.0.50.0030.21314.28
5.0.40.0030.20014.28
5.0.30.0030.22714.28
5.0.20.0070.18714.28
5.0.10.0030.16714.28
5.0.00.0170.21714.28
4.4.90.0030.03314.28
4.4.80.0030.03014.28
4.4.70.0030.03714.28
4.4.60.0000.03014.28
4.4.50.0000.03714.28
4.4.40.0070.05014.28
4.4.30.0070.01014.28
4.4.20.0030.03714.28
4.4.10.0000.02014.28
4.4.00.0030.04014.28
4.3.110.0000.03714.28
4.3.100.0000.03314.28
4.3.90.0000.03314.28
4.3.80.0030.05314.28
4.3.70.0000.03714.28
4.3.60.0100.03014.28
4.3.50.0030.03714.28
4.3.40.0030.05314.28
4.3.30.0000.03714.28
4.3.20.0030.03014.28
4.3.10.0000.03714.28
4.3.00.0000.03714.28

preferences:
33.86 ms | 401 KiB | 5 Q