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.2180140018s should be faster: 0.1911599636s
Output for 7.1.8
should be slower: 0.1923818588s should be faster: 0.1659591198s
Output for 7.1.7
should be slower: 0.1988041401s should be faster: 0.1793370247s
Output for 7.1.6
should be slower: 0.1914160252s should be faster: 0.1629679203s
Output for 7.1.5
should be slower: 0.2299809456s should be faster: 0.1917428970s
Output for 7.1.4
should be slower: 0.1853489876s should be faster: 0.1777570248s
Output for 7.1.3
should be slower: 0.2501449585s should be faster: 0.1745929718s
Output for 7.1.2
should be slower: 0.2101790905s should be faster: 0.1572918892s
Output for 7.1.1
should be slower: 0.2229540348s should be faster: 0.2138180733s
Output for 7.1.0
should be slower: 0.2137529850s should be faster: 0.1662430763s
Output for 7.0.23
should be slower: 0.2221260071s should be faster: 0.2132830620s
Output for 7.0.22
should be slower: 0.2086760998s should be faster: 0.1853790283s
Output for 7.0.21
should be slower: 0.2190098763s should be faster: 0.1827340126s
Output for 7.0.20
should be slower: 0.2853739262s should be faster: 0.2907311916s
Output for 7.0.19
should be slower: 0.2392230034s should be faster: 0.4177320004s
Output for 7.0.18
should be slower: 0.2961478233s should be faster: 0.2096388340s
Output for 7.0.17
should be slower: 0.2002928257s should be faster: 0.1796178818s
Output for 7.0.16
should be slower: 0.2069528103s should be faster: 0.2722890377s
Output for 7.0.15
should be slower: 0.2137150764s should be faster: 0.1721830368s
Output for 7.0.14
should be slower: 0.2112128735s should be faster: 0.2263710499s
Output for 7.0.13
should be slower: 0.2156682014s should be faster: 0.1855950356s
Output for 7.0.12
should be slower: 0.2052068710s should be faster: 0.1746928692s
Output for 7.0.11
should be slower: 0.2043120861s should be faster: 0.1878130436s
Output for 7.0.10
should be slower: 0.2025618553s should be faster: 0.1989119053s
Output for 7.0.9
should be slower: 0.2004520893s should be faster: 0.1993279457s
Output for 7.0.8
should be slower: 0.1915228367s should be faster: 0.1699099541s
Output for 7.0.7
should be slower: 0.1954309940s should be faster: 0.1709170341s
Output for 7.0.6
should be slower: 0.2167890072s should be faster: 0.1806399822s
Output for 7.0.5
should be slower: 0.1890540123s should be faster: 0.1687319279s
Output for 7.0.4
should be slower: 0.2014200687s should be faster: 0.1748130322s
Output for 7.0.3
should be slower: 0.2511858940s should be faster: 0.1766219139s
Output for 7.0.2
should be slower: 0.2020430565s should be faster: 0.1711370945s
Output for 7.0.1
should be slower: 0.2042438984s should be faster: 0.1775519848s
Output for 7.0.0
should be slower: 0.1993510723s should be faster: 0.1726768017s

preferences:
69.43 ms | 401 KiB | 39 Q