3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* Sample Input 100 9999 123456 1000000000 0 Sample Output 21 356 1684 438744 */ // n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34... // opsAddCountDown 0 0 1 0 1 0 2 1 0 2 1.... // opsAddCount 1 2 2 3 3 4 4 4 5 5 5.... // opsAddFut 1 2 2 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 9 10 10 10 10 10 11... // opsAdd +1 +2 . +2 . +3 . . +3 . . +4 . . . +4 . . . +4 . . . +5 . . . . +5 . . . . +5... // sum 1 3 3 5 5 8 8 8 11 11 11 15 15 15 15 19 19 19 19 23 23 23 23 28 28 28 28 28 33 33 33 33 33 38... // goto n(sum+1) 2 4 6 9 12 16 20 24 29 34 39... /** * * @author Starson Hochschild <starsonh@gmail.com> * */ abstract class golomb { /** * V2 - implement conditional array building and count * * @param int $n * @return NULL|number */ public static function f($n) { $n = (int)$n; if($n < 1) return null; $queueOpsAddCountDown = array(); $ops = 0; $opsAdd = 0; $opsAddCountDown = 0; $opsAddFuture = 0; $opsAddDone = true; $predictSum = true; $sum = 0; $sumFuture = 0; while ($sum < $n) { $opsAddDone = $opsAddCountDown == 0; // If we're done with this $opsAdd increment it, separated from the $opsAddDone block below on purpose if($opsAddDone) { $opsAdd++; } // Cache a future $opsAddCountDown if($predictSum) { $opsAddFuture++; $queueOpsAddCountDown[] = $opsAdd; // See if this will be enough to equal or overtake $n $sumFuture += $opsAddFuture * $opsAdd; $predictSum = $sumFuture < $n; } // If we're done with this $opsAdd grab a new $opsAddCountDown from $listOpsAddCountDown if($opsAddDone) { $opsAddCountDown = array_shift($queueOpsAddCountDown); } // Do the real work $sum += $opsAdd; $ops++; $opsAddCountDown--; } return $ops; } public static function fThisList($listN) { $listN = preg_split("/[\s,]+/", $listN); $fN = array(); foreach($listN as $n) $fN[$n] = self::f($n); return $fN; } } $listN = "1 2 3 4 5 6 7 8 9 10 11 12 100 9999 123456 1000000000 2000000000 2147483647"; $fn = 0; if(isset($_GET["listN"])) { $listN = $_GET["listN"]; $start = microtime(true); $fn = golomb::fThisList($listN); $end = microtime(true); } ?> <html> <head></head> <body> <form action="" method="get"> <i>f(<textarea name="listN" rows="15"><?php echo htmlentities($listN); ?></textarea>)</i><br /> <input type="submit" value="Calculate f(n)" /><br /><br /> <i>f(n) =</i> <pre><?php var_export($fn); ?></pre><br /> <?php if(isset($end)) { ?> Combined processing time: <?php echo $end - $start ?> seconds <?php } ?> </form> This page's source:<br /> <?php htmlentities(highlight_file(__FILE__)); ?> </body> </html> <?php /** * This is here for historical purposes. It shows the progression that lead to the method above. * @author Starson Hochschild <starsonh@gmail.com> * */ abstract class golombArchive { /** * V1.1 - added some memory management * Better, but still takes a lot of memory and crashes on large(r) numbers due to running out of memory * * @param int $n * @return NULL|number */ public static function fWithCleanup($n) { $n = (int)$n; if($n < 1) return null; $listFn = array(); $ops = 0; $sum = 0; for($cn = 0; $sum < $n; $cn++) { if($sum < $cn) { $ops++; $listFn[$cn] = $ops; $sum += $listFn[$ops]; // Cleanup unset($listFn[$ops]); if($ops % 4096 == 0) gc_collect_cycles(); } else { $listFn[$cn] = $ops; } } return $ops; } /** * V1 - a simple, "brute force" type of approach * Takes a lot of memory and crashes on large numbers due to running out of memory * * @param int $n * @return NULL|number */ public static function fBrute($n) { $listFn = array(); $n = (int)$n; if($n < 1) return null; $ops = 0; $sum = 0; for($cn = 0; $sum < $n; $cn++) { if($sum < $cn) { $ops++; $listFn[$cn] = $ops; $sum += $listFn[$ops]; } else { $listFn[$cn] = $ops; } } return $ops; } } ?>

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.100.2050.01714.43
7.2.90.1950.00614.38
7.2.80.2330.01014.55
7.2.70.2020.01014.73
7.2.60.1970.00614.86
7.2.50.1900.01314.55
7.2.40.1760.01714.98
7.2.30.1620.01015.20
7.2.20.1590.01214.86
7.2.10.0320.00914.64
7.2.00.0960.00315.06
7.1.220.1960.00713.33
7.1.210.1910.01313.62
7.1.200.2150.01313.39
7.1.190.2450.00313.69
7.1.180.2000.00313.50
7.1.170.2090.01313.82
7.1.160.2070.00313.79
7.1.150.1600.00313.72
7.1.140.1560.01013.72
7.1.130.1670.00713.53
7.1.120.0700.00713.64
7.1.110.2120.01613.54
7.1.100.1320.00613.75
7.1.90.0410.00613.82
7.1.80.0230.00513.77
7.1.70.1830.01313.42
7.1.60.1030.02031.98
7.1.50.1150.00732.00
7.1.40.0460.01331.59
7.1.30.0660.01431.90
7.1.20.1740.00732.08
7.1.10.1170.01013.73
7.1.00.1730.01413.95
7.0.310.1800.01013.59
7.0.300.1790.01313.36
7.0.290.2150.00713.30
7.0.280.3770.01313.27
7.0.270.1790.01913.21
7.0.260.0170.01013.04
7.0.250.0380.01313.43
7.0.240.0310.01613.45
7.0.230.1820.01013.69
7.0.220.0280.01213.76
7.0.210.0290.01713.29
7.0.200.0130.01613.16
7.0.190.0600.01313.76
7.0.180.1610.00313.66
7.0.170.1970.00713.63
7.0.160.1300.01513.59
7.0.150.0400.00713.58
7.0.140.1590.00913.44
7.0.130.0740.01013.47
7.0.120.0850.01413.57
7.0.110.0900.00613.65
7.0.100.1400.00713.65
7.0.90.0430.00313.51
7.0.80.0230.01013.35
7.0.70.0380.00513.40
7.0.60.0220.01613.68
7.0.50.0260.00813.49
7.0.40.0170.01113.41
7.0.30.0220.00313.67
7.0.20.0750.00413.75
7.0.10.1400.00613.35
7.0.00.1140.00713.66
5.6.380.0080.01414.12
5.6.370.0080.01614.31
5.6.360.0220.01314.22
5.6.350.0170.00913.98
5.6.340.0190.00814.29
5.6.330.0190.01614.00
5.6.320.0240.01214.01
5.6.310.0090.01514.24
5.6.300.0170.01714.17
5.6.290.0170.01114.27
5.6.280.0190.01114.13
5.6.270.0180.01014.12
5.6.260.0150.01114.19
5.6.250.0210.01214.44
5.6.240.0220.00814.19
5.6.230.0150.01514.20
5.6.220.0180.00914.32
5.6.210.0060.01914.11
5.6.200.0280.00314.04
5.6.190.0180.01514.60
5.6.180.0150.01014.20
5.6.170.0150.01614.21
5.6.160.0210.00614.25
5.6.150.0250.00314.32
5.6.140.0180.00413.96
5.6.130.0210.01214.34
5.6.120.0080.01614.46
5.6.110.0130.01614.05
5.6.100.0140.01313.97
5.6.90.0210.00313.93
5.6.80.0170.00914.21
5.6.70.0170.00814.10
5.6.60.0180.00913.97
5.6.50.0310.00314.04
5.6.40.0160.01314.21
5.6.30.0190.00814.03
5.6.20.0190.00614.21
5.5.380.0110.01112.61
5.5.370.0150.01212.61
5.5.360.0170.01412.61
5.5.350.0190.00512.61
5.5.340.0150.00612.61
5.5.330.0090.01512.61
5.5.320.0130.00712.61
5.5.310.0080.01112.61
5.5.300.0120.00612.61
5.5.290.0130.01012.61
5.5.280.0100.00812.61
5.5.270.0220.00412.61
5.5.260.0170.00912.61
5.5.250.0170.00612.61
5.5.240.0050.01512.61
5.5.230.0160.00712.61
5.5.220.0120.00812.61
5.5.210.0080.01212.61
5.5.200.0140.00512.61
5.5.190.0130.01312.61
5.4.450.0080.01312.61
5.4.440.0110.00812.61
5.4.430.0180.00312.61
5.4.420.0150.00812.61
5.4.410.0080.01212.61
5.4.400.0150.00312.61
5.4.390.0150.00812.61
5.4.380.0110.00812.61
5.4.370.0140.00612.61
5.4.360.0190.00612.61
5.4.350.0170.00712.61

preferences:
32.46 ms | 400 KiB | 5 Q