3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // The slow, although default way of doing fibonacci --------------------------- function _fibs($n) { if ($n < 2) return $n; else return _fibs($n-1) + _fibs($n-2); } function fibs() { $i = 0; while (1) yield _fibs($i++); } // The speedy way to do fibonacci----------------------------------------------- function fibf() { $a = 0; $b = 0; $t = 1; while (1) { yield $b; if ($b > 0) { $t = $a; $a = $b; } $b += $t; } } // Test utils ------------------------------------------------------------------ // // function to test an iterator by iterating it until the $compare function // returns false // // print the result visually because its sexy function visual_iterator_tester($fn, $cmpr) { echo "timing $fn "; $then = time(); foreach ($fn() as $f) { if ($cmpr($f)) break; echo "."; } $now = time(); $diff = $now - $then; echo "\n$fn completed in $diff second(s)\n"; } // Run the damn thing ---------------------------------------------------------- $fns = ['fibs', 'fibf']; $runs = 9999999; foreach ($fns as $fn) visual_iterator_tester($fn, function($a) use($runs) { return $a > $runs; });
Output for 7.0.0 - 7.0.10, 7.1.0
timing fibs ....................................
Process exited with code 137.
Output for 7.0.14
timing fibs ...................................
Process exited with code 137.
Output for 5.5.27 - 5.5.28, hhvm-3.10.0 - 3.12.0
timing fibs ...............................
Process exited with code 137.
Output for 5.5.38 - 5.6.2, 5.6.4 - 5.6.5, 5.6.8 - 5.6.9, 5.6.11 - 5.6.14, 5.6.18 - 5.6.22, 5.6.24 - 5.6.28
timing fibs .................................
Process exited with code 137.
Output for 5.6.3, 5.6.6 - 5.6.7, 5.6.10, 5.6.15 - 5.6.17, 5.6.23
timing fibs ..................................
Process exited with code 137.
Output for 5.5.0 - 5.5.26, 5.5.29 - 5.5.37
timing fibs ................................
Process exited with code 137.
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '_fibs' (T_STRING) in /in/h9nA3 on line 17
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_STRING in /in/h9nA3 on line 17
Process exited with code 255.