Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Microbenchmark of generator implementation
<?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);
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.
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.
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.
@TuningGuide

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?

@levic

@TuningGuide - were you running a beta version of php?

Mac OS 10.9 i7 I get results broadly in line with those above:
xrange (1000000) took 0.31727600097656 seconds.
RangeIterator (1000000) took 1.3108851909637 seconds.
urange (1000000) took 0.75727391242981 seconds.
range (1000000) took 0.60222411155701 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.