Skip to content

Instantly share code, notes, and snippets.

@nikic
Created June 22, 2012 23:42
Show Gist options
  • Save nikic/2975796 to your computer and use it in GitHub Desktop.
Save nikic/2975796 to your computer and use it in GitHub Desktop.
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.
@fadhil-riyanto
Copy link

hp 430 i3 1st gen. linux 6.8.9, php 8.3.7

xrange        (1000000) took 0.1189968585968 seconds.
RangeIterator (1000000) took 0.27273011207581 seconds.
urange        (1000000) took 0.074035882949829 seconds.
range         (1000000) took 0.029606103897095 seconds.
xrange        (10000) took 0.00069785118103027 seconds.
RangeIterator (10000) took 0.0027949810028076 seconds.
urange        (10000) took 0.0005340576171875 seconds.
range         (10000) took 0.0001990795135498 seconds.
xrange        (100) took 1.1920928955078E-5 seconds.
RangeIterator (100) took 3.3140182495117E-5 seconds.
urange        (100) took 8.8214874267578E-6 seconds.
range         (100) took 6.9141387939453E-6 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment