public
Last active

Microbenchmark of generator implementation

  • Download Gist
microbench.php
PHP
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<?php
 
error_reporting(E_ALL);
 
function xrange($start, $end, $step = 1) {
for ($i = $start; $i < $end; $i += $step) {
yield $i;
}
}
 
class RangeIterator implements Iterator {
protected $start;
protected $end;
protected $step;
protected $key;
protected $value;
public function __construct($start, $end, $step = 1) {
$this->start = $start;
$this->end = $end;
$this->step = $step;
}
public function rewind() {
$this->key = 0;
$this->value = $this->start;
}
public function valid() {
return $this->value < $this->end;
}
public function next() {
$this->value += $this->step;
$this->key += 1;
}
public function current() {
return $this->value;
}
public function key() {
return $this->key;
}
}
 
function urange($start, $end, $step = 1) {
$result = [];
for ($i = $start; $i < $end; $i += $step) {
$result[] = $i;
}
return $result;
}
 
function testTraversable($name, callable $traversableFactory) {
$startTime = microtime(true);
foreach ($traversableFactory() as $value) {
// noop
}
echo $name, ' took ', microtime(true) - $startTime, ' seconds.', "\n";
}
 
function testVariants($count) {
testTraversable(
"xrange ($count)",
function() use($count) { return xrange(0, $count); }
);
testTraversable(
"RangeIterator ($count)",
function() use($count) { return new RangeIterator(0, $count); }
);
testTraversable(
"urange ($count)",
function() use($count) { return urange(0, $count); }
);
testTraversable(
"range ($count)",
function() use($count) { return range(0, $count); }
);
}
 
testVariants(1000000);
testVariants(10000);
testVariants(100);
run1.out
1 2 3 4 5 6 7 8 9 10 11 12
xrange (1000000) took 0.14633202552795 seconds.
RangeIterator (1000000) took 0.5944938659668 seconds.
urange (1000000) took 0.28175210952759 seconds.
range (1000000) took 0.23163318634033 seconds.
xrange (10000) took 0.0015981197357178 seconds.
RangeIterator (10000) took 0.0065171718597412 seconds.
urange (10000) took 0.0028119087219238 seconds.
range (10000) took 0.0022850036621094 seconds.
xrange (100) took 7.8916549682617E-5 seconds.
RangeIterator (100) took 0.00010895729064941 seconds.
urange (100) took 5.5074691772461E-5 seconds.
range (100) took 8.082389831543E-5 seconds.
run2.out
1 2 3 4 5 6 7 8 9 10 11 12
xrange (1000000) took 0.14681696891785 seconds.
RangeIterator (1000000) took 0.60637879371643 seconds.
urange (1000000) took 0.29368591308594 seconds.
range (1000000) took 0.22470617294312 seconds.
xrange (10000) took 0.0014379024505615 seconds.
RangeIterator (10000) took 0.0056979656219482 seconds.
urange (10000) took 0.002910852432251 seconds.
range (10000) took 0.0023188591003418 seconds.
xrange (100) took 5.1021575927734E-5 seconds.
RangeIterator (100) took 0.00014209747314453 seconds.
urange (100) took 5.4121017456055E-5 seconds.
range (100) took 4.3869018554688E-5 seconds.
run3.out
1 2 3 4 5 6 7 8 9 10 11 12
xrange (1000000) took 0.14807891845703 seconds.
RangeIterator (1000000) took 0.60018301010132 seconds.
urange (1000000) took 0.28284788131714 seconds.
range (1000000) took 0.22018909454346 seconds.
xrange (10000) took 0.0013790130615234 seconds.
RangeIterator (10000) took 0.006242036819458 seconds.
urange (10000) took 0.0016648769378662 seconds.
range (10000) took 0.0019738674163818 seconds.
xrange (100) took 9.2029571533203E-5 seconds.
RangeIterator (100) took 0.00013899803161621 seconds.
urange (100) took 5.1975250244141E-5 seconds.
range (100) took 4.3153762817383E-5 seconds.

On a Mac OS 10.6.8 with Intel i5 Quadcore, MAMP, PHP5.5 I got:

xrange (1000000) took 6.64660000801 seconds.
RangeIterator (1000000) took 21.9801108837 seconds.
urange (1000000) took 1.44044995308 seconds.
range (1000000) took 0.779198884964 seconds.

Why is xrange aka generator that much slower than urange and range?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.