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.1248800755s should be faster: 0.1732161045s
Output for 7.1.8
should be slower: 0.1217160225s should be faster: 0.1697740555s
Output for 7.1.7
should be slower: 0.1208600998s should be faster: 0.1701021194s
Output for 7.1.6
should be slower: 0.1262788773s should be faster: 0.2501759529s
Output for 7.1.5
should be slower: 0.1507329941s should be faster: 0.2164978981s
Output for 7.1.4
should be slower: 0.1433079243s should be faster: 0.2295749187s
Output for 7.1.3
should be slower: 0.1251339912s should be faster: 0.2155079842s
Output for 7.1.2
should be slower: 0.2354059219s should be faster: 0.2788958549s
Output for 7.1.1
should be slower: 0.2258279324s should be faster: 0.2867281437s
Output for 7.1.0
should be slower: 0.1263189316s should be faster: 0.1908869743s
Output for 7.0.23
should be slower: 0.1429870129s should be faster: 0.1888790131s
Output for 7.0.22
should be slower: 0.1360440254s should be faster: 0.2126321793s
Output for 7.0.21
should be slower: 0.1598360538s should be faster: 0.2429108620s
Output for 7.0.20
should be slower: 0.2769291401s should be faster: 0.2943069935s
Output for 7.0.19
should be slower: 0.1422491074s should be faster: 0.2268540859s
Output for 7.0.18
should be slower: 0.1669778824s should be faster: 0.2402698994s
Output for 7.0.17
should be slower: 0.2430510521s should be faster: 0.1984100342s
Output for 7.0.16
should be slower: 0.1418089867s should be faster: 0.2304151058s
Output for 7.0.15
should be slower: 0.2377371788s should be faster: 0.3262858391s
Output for 7.0.14
should be slower: 0.1896648407s should be faster: 0.2277739048s
Output for 7.0.13
should be slower: 0.1336190701s should be faster: 0.1742079258s
Output for 7.0.12
should be slower: 0.1318809986s should be faster: 0.1783728600s
Output for 7.0.11
should be slower: 0.1462941170s should be faster: 0.1730630398s
Output for 7.0.10
should be slower: 0.1364140511s should be faster: 0.2110021114s
Output for 7.0.9
should be slower: 0.1313180923s should be faster: 0.2013590336s
Output for 7.0.8
should be slower: 0.1311480999s should be faster: 0.1693930626s
Output for 7.0.7
should be slower: 0.1418519020s should be faster: 0.1764450073s
Output for 7.0.6
should be slower: 0.1294491291s should be faster: 0.1825408936s
Output for 7.0.5
should be slower: 0.1398007870s should be faster: 0.2633628845s
Output for 7.0.4
should be slower: 0.1502540112s should be faster: 0.1824400425s
Output for 7.0.3
should be slower: 0.1313869953s should be faster: 0.1728708744s
Output for 7.0.2
should be slower: 0.1371090412s should be faster: 0.1686878204s
Output for 7.0.1
should be slower: 0.1351180077s should be faster: 0.1842241287s
Output for 7.0.0
should be slower: 0.1663589478s should be faster: 0.1989748478s

preferences:
73.93 ms | 401 KiB | 39 Q