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)
8.2.00.0030.01418.08
8.0.30.0200.02516.98
8.0.20.0190.03017.16
8.0.10.0180.03417.19
8.0.00.0170.03417.21
7.4.160.0130.02616.70
7.4.150.0210.02416.65
7.4.140.0200.02116.74
7.4.130.0150.03116.60
7.4.120.0120.02916.60
7.4.110.0150.03116.69
7.4.100.0160.03016.79
7.4.90.0150.02916.68
7.4.80.0090.03116.63
7.4.70.0140.02416.63
7.4.60.0140.02716.78
7.4.50.0150.02216.61
7.4.40.0140.02316.48
7.4.30.0170.02416.68
7.4.20.0140.03016.59
7.4.10.0210.02316.61
7.4.00.0130.02416.61
7.3.280.0120.02716.48
7.3.270.0190.02916.57
7.3.260.0160.04016.55
7.3.250.0130.02916.57
7.3.240.0200.02416.53
7.3.230.0100.03816.50
7.3.220.0190.02716.43
7.3.210.0180.03116.52
7.3.200.0190.03216.46
7.3.190.0270.01916.48
7.3.180.0130.03216.53
7.3.170.0140.02716.41
7.3.160.0170.03116.43
7.3.150.0130.02916.54
7.3.140.0200.02216.49
7.3.130.0150.02716.52
7.3.120.0130.02916.50
7.3.110.0130.04116.44
7.3.100.0160.03216.34
7.3.90.0100.03616.76
7.3.80.0270.01916.50
7.3.70.0220.02616.56
7.3.60.0240.02616.71
7.3.50.0150.03416.65
7.3.40.0230.02916.63
7.3.30.0150.03716.58
7.3.20.0170.03016.63
7.3.10.0130.02816.83
7.3.00.0100.02716.79
7.2.340.0210.03416.59
7.2.330.0180.03316.52
7.2.320.0250.03616.62
7.2.310.0180.03816.55
7.2.300.0170.03216.54
7.2.290.0150.03416.62
7.2.280.0160.03416.55
7.2.270.0210.03516.63
7.2.260.0240.02716.52
7.2.250.0260.03416.64
7.2.240.0220.02716.59
7.2.230.0260.02416.57
7.2.220.0230.03616.61
7.2.210.0210.03916.54
7.2.200.0250.03616.84
7.2.190.0220.03316.72
7.2.180.0220.03516.71
7.2.170.0190.03516.79
7.2.160.0170.03616.80
7.2.150.0220.02416.82
7.2.140.0200.03216.93
7.2.130.0100.03217.11
7.2.120.0140.02917.04
7.2.110.0130.02817.04
7.2.100.0120.03117.00
7.2.90.0150.03317.07
7.2.80.0150.03116.96
7.2.70.0300.02916.60
7.2.60.0340.03016.63
7.2.50.0300.03016.62
7.2.40.0290.03016.65
7.2.30.0280.02716.65
7.2.20.0260.02816.69
7.2.10.0270.02816.71
7.2.00.0210.03116.57
7.1.330.0210.03216.02
7.1.320.0220.03116.02
7.1.310.0230.04316.02
7.1.300.0200.03116.02
7.1.290.0220.04016.02
7.1.280.0200.03116.02
7.1.270.0260.03516.02
7.1.260.0210.03616.02
7.1.250.0120.03615.99
7.1.240.0210.03216.02
7.1.230.0190.04416.02
7.1.220.0240.02716.02
7.1.210.0230.03216.02
7.1.200.0160.03116.02
7.1.190.0220.03316.02
7.1.180.0150.03516.02
7.1.170.0400.02515.49
7.1.160.0390.02915.47
7.1.150.0360.03215.41
7.1.140.0380.03715.37
7.1.130.0360.03615.46
7.1.120.0360.03515.35
7.1.110.0390.02815.37
7.1.100.0260.03515.61
7.1.90.0350.03515.65
7.1.80.1910.03915.69
7.1.70.0180.02815.80
7.1.60.0260.04023.41
7.1.50.1250.03622.94
7.1.40.0280.03424.29
7.1.30.1890.03624.32
7.1.20.0380.04224.37
7.1.10.0400.03515.28
7.1.00.0470.05416.71
7.0.330.0120.03816.02
7.0.320.0220.04516.02
7.0.310.0250.04316.02
7.0.300.0340.03815.32
7.0.290.0390.03815.24
7.0.280.0210.03616.02
7.0.270.0390.03415.27
7.0.260.0470.04015.24
7.0.250.0310.03315.31
7.0.240.0530.03215.59
7.0.230.0350.03315.55
7.0.220.0360.03515.45
7.0.210.0240.03015.21
7.0.200.0360.03515.66
7.0.190.1180.03415.30
7.0.180.1030.03015.07
7.0.170.0210.03315.14
7.0.160.0330.03215.08
7.0.150.0210.03115.13
7.0.140.0600.04416.56
7.0.130.0580.04015.23
7.0.120.0660.03415.14
7.0.110.0210.03615.18
7.0.100.0220.03515.18
7.0.90.0210.03315.19
7.0.80.0780.03515.11
7.0.70.0740.02615.12
7.0.60.0610.04316.17
7.0.50.0440.04715.81
7.0.40.0590.04715.72
7.0.30.0520.05215.68
7.0.20.0650.04015.71
7.0.10.0640.04815.71
7.0.00.0580.05315.79
5.6.400.0170.09816.12
5.6.390.0240.08016.09
5.6.380.0310.10916.08
5.6.370.0180.10816.08
5.6.360.0180.10116.10
5.6.350.0220.08416.12
5.6.340.0250.11216.08
5.6.330.0170.09216.14
5.6.320.0170.10416.12
5.6.310.0260.12016.12
5.6.300.0140.11016.85
5.6.290.0180.10217.01
5.6.280.0160.12817.89
5.6.270.0150.11216.98
5.6.260.0190.10316.84
5.6.250.0170.11417.08
5.6.240.0190.11517.00
5.6.230.0110.09717.14
5.6.220.0160.09917.05
5.6.210.0150.10617.88
5.6.200.0140.11017.36
5.6.190.0180.12317.30
5.6.180.0160.11717.35
5.6.170.0170.11417.42
5.6.160.0160.12317.40
5.6.150.0170.11617.34
5.6.140.0170.11817.28
5.6.130.0150.11617.37
5.6.120.0160.11917.32
5.6.110.0190.12217.27
5.6.100.0120.12017.26
5.6.90.0170.11017.21
5.6.80.0120.11017.01
5.6.70.0130.10917.02
5.6.60.0150.10316.96
5.6.50.0130.10117.06
5.6.40.0180.11517.05
5.6.30.0140.11617.03
5.6.20.0120.11316.99
5.6.10.0120.10717.04
5.6.00.0150.09917.02
5.5.380.0130.09615.36
5.5.370.0160.12315.34
5.5.360.0160.12215.19
5.5.350.0170.10916.37
5.5.340.0130.10915.93
5.5.330.0140.10216.00
5.5.320.0160.10316.03
5.5.310.0170.11116.03
5.5.300.0120.12515.99
5.5.290.0110.10515.96
5.5.280.0150.12116.03
5.5.270.0120.11515.98
5.5.260.0150.10515.90
5.5.250.0150.09615.89
5.5.240.0140.11315.83
5.5.230.0140.09915.80
5.5.220.0120.10815.66
5.5.210.0150.10515.76
5.5.200.0130.10415.72
5.5.190.0110.10415.73
5.5.180.0130.11215.72
5.5.170.0210.10016.09
5.5.160.0150.09515.66
5.5.150.0110.12115.64
5.5.140.0090.10115.64
5.5.130.0120.09815.72
5.5.120.0130.10015.68
5.5.110.0140.10115.71
5.5.100.0100.11815.57
5.5.90.0120.11715.63
5.5.80.0130.10215.72
5.5.70.0110.10515.64
5.5.60.0100.10015.67
5.5.50.0130.10215.64
5.5.40.0150.11915.62
5.5.30.0140.10515.66
5.5.20.0130.09915.72
5.5.10.0150.09615.62
5.5.00.0150.11315.70
5.4.450.0120.10716.55
5.4.440.0200.11216.54
5.4.430.0130.11516.54
5.4.420.0110.10316.51
5.4.410.0080.11316.47
5.4.400.0130.11816.26
5.4.390.0110.10016.30
5.4.380.0100.11216.44
5.4.370.0160.08816.32
5.4.360.0070.09416.41
5.4.350.0150.10116.25
5.4.340.0110.12216.31
5.4.330.0190.11816.02
5.4.320.0100.11516.38
5.4.310.0140.09716.38
5.4.300.0130.09616.40
5.4.290.0160.09416.31
5.4.280.0150.10116.35
5.4.270.0100.10316.33
5.4.260.0120.10616.35
5.4.250.0080.09416.23
5.4.240.0120.10616.32
5.4.230.0120.11816.43
5.4.220.0090.10816.35
5.4.210.0090.11416.33
5.4.200.0110.12016.37
5.4.190.0140.09716.38
5.4.180.0130.10516.23
5.4.170.0130.10516.37
5.4.160.0140.11516.39
5.4.150.0130.10516.33
5.4.140.0150.10115.30
5.4.130.0100.11015.38
5.4.120.0120.10915.39
5.4.110.0090.09515.40
5.4.100.0120.09915.33
5.4.90.0150.10115.35
5.4.80.0140.10615.40
5.4.70.0150.11915.40
5.4.60.0110.12115.34
5.4.50.0100.10115.46
5.4.40.0070.10415.32
5.4.30.0120.10615.34
5.4.20.0120.11015.27
5.4.10.0120.09915.41
5.4.00.0150.09915.15
5.3.290.0150.14814.52
5.3.280.0100.13614.51
5.3.270.0090.14314.52
5.3.260.0120.14214.47
5.3.250.0130.16514.49
5.3.240.0100.13614.54
5.3.230.0120.14014.50
5.3.220.0120.14414.47
5.3.210.0170.13914.43
5.3.200.0120.15414.51
5.3.190.0080.14214.46
5.3.180.0110.15114.41
5.3.170.0140.16314.48
5.3.160.0100.15714.47
5.3.150.0140.12114.48
5.3.140.0140.14814.50
5.3.130.0120.15314.53
5.3.120.0130.14014.53
5.3.110.0180.13514.47
5.3.100.0110.15114.28
5.3.90.0090.14614.30
5.3.80.0110.13514.28
5.3.70.0120.13814.29
5.3.60.0140.16314.26
5.3.50.0120.13614.30
5.3.40.0120.15014.28
5.3.30.0130.15514.26
5.3.20.0090.13314.11
5.3.10.0150.15514.00
5.3.00.0130.15714.07

preferences:
38.75 ms | 400 KiB | 5 Q