3v4l.org

run code in 300+ PHP versions simultaneously
<?php class IteratorV1 implements Iterator { private $index; public $table; function __construct(SplFixedArray $table){ $this->table = $table; $this->rewind(); } function rewind(){ $this->index = -1; $this->next(); } function next(){ while( ++$this->index < $this->table->count() && $this->table->offsetGet($this->index) === NULL ); } function valid(){ return $this->index < $this->table->count(); } function key(){ return $this->table->offsetGet($this->index)->uncommentMe; } function current(){ return $this->table->offsetGet($this->index)->uncommentMe; } } class IteratorV2 implements Iterator { private $index; private $array; private $entry; function __construct(SplFixedArray $array){ $this->array = $array; $this->rewind(); } function rewind(){ for( $this->index = 0; $this->index < $this->array->count(); $this->index++ ){ $this->entry = $this->array->offsetGet($this->index); if($this->entry !== NULL){ return; } } $this->entry = NULL; } function next(){ for( $this->index++; $this->index < $this->array->count(); $this->index++ ){ $this->entry = $this->array->offsetGet($this->index); if($this->entry !== NULL){ return; } } $this->entry = NULL; } function valid(){ return $this->entry !== NULL; } function key(){ return $this->entry->uncommentMe; } function current(){ return $this->entry->uncommentMe; } } class Entry{ public $uncommentMe = 123; } $array = new SplFixedArray(2 ** 18); for($i = 0; $i < (2 ** 18) / 2; $i++){ $array->offsetSet($i * 2, new Entry); } echo "should be slower:\n"; $st = microtime(true); foreach(new IteratorV1($array) as $key => $content){} echo number_format(microtime(true) - $st, 10) . "s\n"; echo "should be faster:\n"; $st = microtime(true); foreach(new IteratorV2($array) as $key => $content){} echo number_format(microtime(true) - $st, 10) . "s\n";
Output for 7.1.9
should be slower: 0.1238920689s should be faster: 0.1744971275s
Output for 7.1.8
should be slower: 0.1202249527s should be faster: 0.1634619236s
Output for 7.1.7
should be slower: 0.1213901043s should be faster: 0.1755311489s
Output for 7.1.6
should be slower: 0.1211609840s should be faster: 0.2326629162s
Output for 7.1.5
should be slower: 0.1205151081s should be faster: 0.1750900745s
Output for 7.1.4
should be slower: 0.1330058575s should be faster: 0.2064530849s
Output for 7.1.3
should be slower: 0.1311910152s should be faster: 0.1620049477s
Output for 7.1.2
should be slower: 0.1269509792s should be faster: 0.1589879990s
Output for 7.1.1
should be slower: 0.1197161674s should be faster: 0.1621639729s
Output for 7.1.0
should be slower: 0.1522469521s should be faster: 0.2300350666s
Output for 7.0.23
should be slower: 0.1467051506s should be faster: 0.1910989285s
Output for 7.0.22
should be slower: 0.1331298351s should be faster: 0.1785428524s
Output for 7.0.21
should be slower: 0.1442620754s should be faster: 0.1847319603s
Output for 7.0.20
should be slower: 0.1412279606s should be faster: 0.2025671005s
Output for 7.0.19
should be slower: 0.1362941265s should be faster: 0.1788129807s
Output for 7.0.18
should be slower: 0.1303341389s should be faster: 0.1723098755s
Output for 7.0.17
should be slower: 0.1404759884s should be faster: 0.1725080013s
Output for 7.0.16
should be slower: 0.1368660927s should be faster: 0.1901581287s
Output for 7.0.15
should be slower: 0.1375808716s should be faster: 0.1712901592s
Output for 7.0.14
should be slower: 0.1458051205s should be faster: 0.1823260784s
Output for 7.0.13
should be slower: 0.1554269791s should be faster: 0.1670670509s
Output for 7.0.12
should be slower: 0.1552779675s should be faster: 0.1829760075s
Output for 7.0.11
should be slower: 0.1774730682s should be faster: 0.2154219151s
Output for 7.0.10
should be slower: 0.1355590820s should be faster: 0.1720781326s
Output for 7.0.9
should be slower: 0.1308810711s should be faster: 0.1825180054s
Output for 7.0.8
should be slower: 0.1661880016s should be faster: 0.1878988743s
Output for 7.0.7
should be slower: 0.1345350742s should be faster: 0.1730089188s
Output for 7.0.6
should be slower: 0.1387360096s should be faster: 0.2481279373s
Output for 7.0.5
should be slower: 0.2294771671s should be faster: 0.2354249954s
Output for 7.0.4
should be slower: 0.1391520500s should be faster: 0.1766510010s
Output for 7.0.3
should be slower: 0.1385040283s should be faster: 0.1830699444s
Output for 7.0.2
should be slower: 0.1468729973s should be faster: 0.1853961945s
Output for 7.0.1
should be slower: 0.2035930157s should be faster: 0.1969158649s
Output for 7.0.0
should be slower: 0.1381111145s should be faster: 0.1743619442s

preferences:
72.79 ms | 401 KiB | 39 Q