<?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));
- Output for 7.2.0
- Generator is Slower than normal function 43.73%.
- Output for 7.1.12
- Generator is Slower than normal function 21.64%.
- Output for 7.1.11
- Generator is Slower than normal function 10.20%.
- Output for 7.1.10
- Generator is Slower than normal function 33.69%.
- Output for 7.1.9
- Generator is Slower than normal function 35.90%.
- Output for 7.1.8
- Generator is Slower than normal function 84.84%.
- Output for 7.1.4, 7.1.7
- Generator is Slower than normal function 40.95%.
- Output for 7.1.6
- Generator is Slower than normal function 41.40%.
- Output for 7.1.5
- Generator is Slower than normal function 28.10%.
- Output for 7.1.3
- Generator is Slower than normal function 178.45%.
- Output for 7.1.2
- Generator is Slower than normal function 127.42%.
- Output for 7.1.1
- Generator is Slower than normal function 76.63%.
- Output for 7.1.0
- Generator is Slower than normal function 112.29%.
preferences:
60.15 ms | 401 KiB | 19 Q