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.10.0160.02516.32
7.3.00.0080.02616.61
7.2.130.0110.03017.08
7.2.120.0080.02817.10
7.2.110.0080.02717.04
7.2.100.0110.02417.14
7.2.90.0070.02817.09
7.2.80.0050.03316.72
7.2.70.0130.02616.88
7.2.60.0080.02716.93
7.2.50.0140.02616.70
7.2.40.0070.03217.07
7.2.30.0130.02816.86
7.2.20.0140.02416.96
7.2.10.0070.03416.75
7.2.00.0050.03018.05
7.1.250.0070.04215.86
7.1.240.0030.03816.20
7.1.230.0000.04516.21
7.1.220.0030.04915.65
7.1.210.0030.04516.03
7.1.200.0030.04216.14
7.1.190.0030.03616.18
7.1.180.0030.03815.84
7.1.170.0030.04115.76
7.1.160.0070.03815.97
7.1.150.0070.04016.03
7.1.140.0030.04416.12
7.1.130.0070.04115.73
7.1.120.0070.03315.85
7.1.110.0100.03516.16
7.1.100.0030.04316.14
7.1.90.0000.04416.14
7.1.80.0040.04215.95
7.1.70.0050.03016.44
7.1.60.0070.04517.74
7.1.50.0030.03216.41
7.1.40.0060.04515.82
7.1.30.0070.04016.06
7.1.20.0060.03915.93
7.1.10.0030.04216.01
7.1.00.0030.06719.41
7.0.330.0070.03415.46
7.0.320.0040.04615.51
7.0.310.0030.04215.78
7.0.300.0000.04815.52
7.0.290.0070.03915.36
7.0.280.0070.03815.59
7.0.270.0030.04115.70
7.0.260.0040.04615.42
7.0.250.0030.03815.66
7.0.240.0030.03815.57
7.0.230.0060.04815.44
7.0.220.0070.05715.39
7.0.210.0000.05115.54
7.0.200.0000.03316.18
7.0.190.0030.04115.58
7.0.180.0030.04315.48
7.0.170.0130.03515.70
7.0.160.0030.04515.43
7.0.150.0030.03815.31
7.0.140.0020.07318.75
7.0.130.0130.03215.20
7.0.120.0050.05118.88
7.0.110.0070.03615.76
7.0.100.0090.03515.41
7.0.90.0000.04815.37
7.0.80.0060.03915.32
7.0.70.0070.03615.63
7.0.60.0030.05217.89
7.0.50.0070.06716.88
7.0.40.0050.05117.00
7.0.30.0150.06116.92
7.0.20.0180.07116.98
7.0.10.0070.07216.79
7.0.00.0070.04816.89
5.6.380.0070.09314.72
5.6.370.0000.11214.52
5.6.360.0070.09614.91
5.6.350.0030.09914.73
5.6.340.0030.09914.61
5.6.330.0030.10314.26
5.6.320.0100.09814.71
5.6.310.0030.10114.53
5.6.300.0030.10314.76
5.6.290.0070.09714.31
5.6.280.0100.09414.63
5.6.270.0030.10114.32
5.6.260.0130.09914.70
5.6.250.0070.10414.46
5.6.240.0070.09614.58
5.6.230.0000.10214.73
5.6.220.0030.09514.79
5.6.210.0070.11417.72
5.6.200.0120.11616.46
5.6.190.0070.09817.62
5.6.180.0100.09317.38
5.6.170.0180.10117.49
5.6.160.0070.10017.74
5.6.150.0080.11616.53
5.6.140.0080.10916.61
5.6.130.0070.10616.42
5.6.120.0150.11917.97
5.6.110.0150.11217.86
5.6.100.0130.11717.82
5.6.90.0050.11118.05
5.6.80.0130.11817.54
5.6.70.0100.10414.79
5.6.60.0100.09114.52
5.6.50.0000.10414.57
5.6.40.0070.08914.43
5.6.30.0070.09514.89
5.6.20.0070.09214.54
5.6.10.0070.10014.36
5.6.00.0070.09514.66
5.5.380.0030.09311.52
5.5.370.0000.10411.63
5.5.360.0000.10210.98
5.5.350.0200.10316.02
5.5.340.0080.09714.65
5.5.330.0020.10715.90
5.5.320.0130.11315.95
5.5.310.0170.12515.86
5.5.300.0000.09314.79
5.5.290.0050.11815.02
5.5.280.0030.12916.25
5.5.270.0000.10416.36
5.5.260.0050.10116.16
5.5.250.0030.09616.19
5.5.240.1050.08315.74
5.5.230.0000.11511.36
5.5.220.0070.09211.49
5.5.210.0030.10311.16
5.5.200.0030.09611.55
5.5.190.0100.09811.18
5.5.180.0130.09111.10
5.5.170.0030.09511.55
5.5.160.0100.10211.23
5.5.150.0000.10511.33
5.5.140.0000.11511.25
5.5.130.0030.09911.27
5.5.120.0030.10511.64
5.5.110.0030.09911.26
5.5.100.0030.10211.32
5.5.90.0000.10711.49
5.5.80.0100.09711.59
5.5.70.0030.10911.42
5.5.60.0000.09711.33
5.5.50.0030.09811.39
5.5.40.0000.11811.13
5.5.30.0000.10111.42
5.5.20.0100.09311.26
5.5.10.0070.09811.19
5.5.00.0000.09911.15
5.4.450.0050.09715.39
5.4.440.0050.12015.59
5.4.430.0070.09515.31
5.4.420.0070.11915.46
5.4.410.0030.09115.50
5.4.400.0030.11815.15
5.4.390.0050.12215.32
5.4.380.0080.11815.27
5.4.370.0030.10815.14
5.4.360.0080.11915.41
5.4.350.0070.11515.13
5.4.340.0070.11515.23
5.4.330.0030.11411.23
5.4.320.0030.10615.32
5.4.310.0080.10415.32
5.4.300.0050.09515.41
5.4.290.0070.11215.37
5.4.280.0020.09514.98
5.4.270.0050.11715.35
5.4.260.0050.12215.18
5.4.250.0030.11315.28
5.4.240.0030.12415.17
5.4.230.0050.09114.94
5.4.220.0080.11915.25
5.4.210.0030.12415.21
5.4.200.0100.12115.20
5.4.190.0070.11015.46
5.4.180.0050.12015.34
5.4.170.0070.11715.09
5.4.160.0050.12015.17
5.4.150.0050.12315.21
5.4.140.0030.12113.99
5.4.130.0120.11413.99
5.4.120.0050.12613.82
5.4.110.0030.11013.96
5.4.100.0100.12113.86
5.4.90.0020.10014.02
5.4.80.0080.12014.00
5.4.70.0050.10114.01
5.4.60.0030.11513.73
5.4.50.0050.12014.04
5.4.40.0020.09713.94
5.4.30.0020.10513.99
5.4.20.0030.10413.86
5.4.10.0030.10714.12
5.4.00.0050.10213.71
5.3.290.0020.12712.69
5.3.280.0050.15312.79
5.3.270.0080.14312.83
5.3.260.0050.14712.91
5.3.250.0050.11912.88
5.3.240.0050.15312.71
5.3.230.0050.12412.69
5.3.220.0030.14612.71
5.3.210.0030.13912.81
5.3.200.0030.12912.75
5.3.190.0030.12812.82
5.3.180.0120.12912.82
5.3.170.0050.13212.71
5.3.160.0050.13412.88
5.3.150.0030.15112.73
5.3.140.0050.15012.83
5.3.130.0030.15312.76
5.3.120.0070.14312.75
5.3.110.0050.13512.77
5.3.100.0070.14712.41
5.3.90.0020.13312.56
5.3.80.0030.13312.39
5.3.70.0030.13712.43
5.3.60.0030.13912.56
5.3.50.0080.14012.40
5.3.40.0000.15012.48
5.3.30.0080.14212.47
5.3.20.0000.14612.30
5.3.10.0080.14712.04
5.3.00.0120.11512.24
5.2.170.0050.14210.61
5.2.160.0030.17510.65
5.2.150.0030.18010.54
5.2.140.0070.14810.44
5.2.130.0080.16710.47
5.2.120.0000.17510.58
5.2.110.0050.16910.49
5.2.100.0080.16810.46
5.2.90.0050.14510.29
5.2.80.0030.14010.46
5.2.70.0120.13110.33
5.2.60.0000.14010.36
5.2.50.0030.14410.23
5.2.40.0050.13710.26
5.2.30.0020.13810.33
5.2.20.0000.13810.39
5.2.10.0030.14610.29
5.2.00.0030.1379.95
5.1.60.0050.1329.45
5.1.50.0020.1399.40
5.1.40.0050.1419.37
5.1.30.0020.1439.72
5.1.20.0070.1489.81
5.1.10.0030.1389.55
5.1.00.0030.1389.54
5.0.50.0050.2028.28
5.0.40.0030.2098.09
5.0.30.0030.2047.96
5.0.20.0050.2058.01
5.0.10.0030.2007.95
5.0.00.0000.2307.92
4.4.90.0010.0097.59
4.4.80.0010.0117.59
4.4.70.0070.0057.59
4.4.60.0060.0057.59
4.4.50.0020.0087.59
4.4.40.0020.0127.59
4.4.30.0000.0117.59
4.4.20.0020.0087.59
4.4.10.0040.0077.59
4.4.00.0000.0157.59
4.3.110.0010.0087.59
4.3.100.0020.0107.59
4.3.90.0040.0077.59
4.3.80.0020.0137.59
4.3.70.0000.0087.59
4.3.60.0020.0087.59
4.3.50.0020.0087.59
4.3.40.0000.0167.59
4.3.30.0020.0137.59
4.3.20.0010.0117.59
4.3.10.0010.0167.59
4.3.00.0020.0197.59

preferences:
30.72 ms | 401 KiB | 5 Q