@ 2017-12-06T05:30:34Z <?php
function xrange($start, $end, $step = 1) {
while ($start <= $end) {
yield $start;
$start += $step;
}
}
function reduce(Generator $iterator, Callable $callback, $initial = null) {
$result = $initial;
foreach($iterator as $value) {
$result = $callback($result, $value);
}
return $result;
}
$start = microtime(true);
reduce(xrange(1, 140000), function($r, $v) {return $r + $v;}, 0);
$byGen = microtime(true) - $start;
$start =microtime(true);
array_reduce(range(1,140000), function($r, $v) {return $r + $v;}, 0);
$byNormal = microtime(true) - $start;
$percent = ($byGen - $byNormal)*100/$byNormal;
printf("Generator is %s than normal function %.2f%%.\n", ($byGen > $byNormal ? 'Slower' : 'Faster'), ($percent));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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).
Version System time (s) User time (s) Memory (MiB) 7.2.0 0.007 0.036 25.12 7.1.12 0.010 0.036 24.68 7.1.11 0.006 0.042 24.34 7.1.10 0.003 0.041 24.27 7.1.9 0.006 0.036 23.96 7.1.8 0.013 0.053 23.97 7.1.7 0.009 0.037 23.10 7.1.6 0.026 0.030 41.34 7.1.5 0.016 0.039 41.17 7.1.4 0.011 0.048 40.47 7.1.3 0.020 0.050 40.54 7.1.2 0.028 0.060 40.58 7.1.1 0.013 0.056 22.57 7.1.0 0.007 0.049 22.61
preferences:dark mode live preview
30.56 ms | 400 KiB | 5 Q