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.4.0
should be slower: 0.2016220093s should be faster: 0.1548418999s
Output for 7.3.12
should be slower: 0.2017390728s should be faster: 0.1569981575s
Output for 7.3.11
should be slower: 0.2034449577s should be faster: 0.1546778679s
Output for 7.3.10
should be slower: 0.1352419853s should be faster: 0.1118907928s
Output for 7.3.9
should be slower: 0.1612138748s should be faster: 0.1527218819s
Output for 7.3.8
should be slower: 0.1311149597s should be faster: 0.1129529476s
Output for 7.3.7
should be slower: 0.1879088879s should be faster: 0.1101038456s
Output for 7.3.6
should be slower: 0.1202249527s should be faster: 0.1027970314s
Output for 7.3.5
should be slower: 0.1481809616s should be faster: 0.1069891453s
Output for 7.3.4
should be slower: 0.1859519482s should be faster: 0.1532700062s
Output for 7.3.3
should be slower: 0.1334519386s should be faster: 0.1213581562s
Output for 7.3.2
should be slower: 0.1331949234s should be faster: 0.0986552238s
Output for 7.3.1
should be slower: 0.1114680767s should be faster: 0.0979268551s
Output for 7.3.0
should be slower: 0.1261780262s should be faster: 0.1099710464s
Output for 7.2.25
should be slower: 0.2466440201s should be faster: 0.1843111515s
Output for 7.2.24
should be slower: 0.2467610836s should be faster: 0.1804099083s
Output for 7.2.23
should be slower: 0.1736650467s should be faster: 0.1904211044s
Output for 7.2.22
should be slower: 0.1674141884s should be faster: 0.1658818722s
Output for 7.2.21
should be slower: 0.1530859470s should be faster: 0.1931960583s
Output for 7.2.20
should be slower: 0.1801950932s should be faster: 0.1731371880s
Output for 7.2.19
should be slower: 0.1692259312s should be faster: 0.1392960548s
Output for 7.2.18
should be slower: 0.2029039860s should be faster: 0.2139480114s
Output for 7.2.17
should be slower: 0.2394387722s should be faster: 0.1510620117s
Output for 7.1.33
should be slower: 0.2159650326s should be faster: 0.1758580208s
Output for 7.1.32
should be slower: 0.2571640015s should be faster: 0.1644651890s
Output for 7.1.31
should be slower: 0.2108628750s should be faster: 0.2306449413s
Output for 7.1.30
should be slower: 0.2583520412s should be faster: 0.2399349213s
Output for 7.1.29
should be slower: 0.2065598965s should be faster: 0.2122640610s
Output for 7.1.28
should be slower: 0.1837289333s should be faster: 0.1926229000s
Output for 7.1.27
should be slower: 0.2163958549s should be faster: 0.1525971889s
Output for 7.1.26
should be slower: 0.1800730228s should be faster: 0.1509311199s
Output for 7.1.9
should be slower: 0.1840360165s should be faster: 0.1627080441s
Output for 7.1.8
should be slower: 0.1886069775s should be faster: 0.1626269817s
Output for 7.1.7
should be slower: 0.1783950329s should be faster: 0.1599829197s
Output for 7.1.6
should be slower: 0.1818180084s should be faster: 0.1579568386s
Output for 7.1.5
should be slower: 0.1724531651s should be faster: 0.1571938992s
Output for 7.1.4
should be slower: 0.1842961311s should be faster: 0.1629359722s
Output for 7.1.3
should be slower: 0.1940131187s should be faster: 0.1535899639s
Output for 7.1.2
should be slower: 0.2510209084s should be faster: 0.1646590233s
Output for 7.1.1
should be slower: 0.2245190144s should be faster: 0.1616890430s
Output for 7.1.0
should be slower: 0.1844439507s should be faster: 0.1863360405s
Output for 7.0.23
should be slower: 0.2021241188s should be faster: 0.1727628708s
Output for 7.0.22
should be slower: 0.2028980255s should be faster: 0.1909730434s
Output for 7.0.21
should be slower: 0.2046470642s should be faster: 0.1812288761s
Output for 7.0.20
should be slower: 0.1912350655s should be faster: 0.1723871231s
Output for 7.0.19
should be slower: 0.1902658939s should be faster: 0.2081959248s
Output for 7.0.18
should be slower: 0.1903679371s should be faster: 0.1697931290s
Output for 7.0.17
should be slower: 0.1950848103s should be faster: 0.1779949665s
Output for 7.0.16
should be slower: 0.2044510841s should be faster: 0.1867899895s
Output for 7.0.15
should be slower: 0.2003488541s should be faster: 0.1872069836s
Output for 7.0.14
should be slower: 0.2577540874s should be faster: 0.1733319759s
Output for 7.0.13
should be slower: 0.2038969994s should be faster: 0.1733288765s
Output for 7.0.12
should be slower: 0.2466700077s should be faster: 0.1797530651s
Output for 7.0.11
should be slower: 0.2369258404s should be faster: 0.1748929024s
Output for 7.0.10
should be slower: 0.1983890533s should be faster: 0.1996719837s
Output for 7.0.9
should be slower: 0.1997628212s should be faster: 0.2169940472s
Output for 7.0.8
should be slower: 0.1985058784s should be faster: 0.1671330929s
Output for 7.0.7
should be slower: 0.2043950558s should be faster: 0.1779301167s
Output for 7.0.6
should be slower: 0.1954269409s should be faster: 0.2586028576s
Output for 7.0.5
should be slower: 0.2052958012s should be faster: 0.1796770096s
Output for 7.0.4
should be slower: 0.3089458942s should be faster: 0.2343919277s
Output for 7.0.3
should be slower: 0.3009510040s should be faster: 0.1763749123s
Output for 7.0.2
should be slower: 0.4667069912s should be faster: 0.2078580856s
Output for 7.0.1
should be slower: 0.2141449451s should be faster: 0.2084801197s
Output for 7.0.0
should be slower: 0.2250669003s should be faster: 0.1700401306s

preferences:
84.72 ms | 401 KiB | 70 Q