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";

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
7.1.90.1030.33629.94
7.1.80.1960.32329.28
7.1.70.1260.33928.41
7.1.60.1520.39446.78
7.1.50.1220.33246.61
7.1.40.1460.36846.25
7.1.30.1280.34546.28
7.1.20.1220.32346.43
7.1.10.1140.31928.00
7.1.00.1850.42028.07
7.0.230.1020.36929.45
7.0.220.1010.34029.33
7.0.210.1070.37028.16
7.0.200.1810.38328.50
7.0.190.1100.34228.09
7.0.180.1470.33427.58
7.0.170.1120.34327.77
7.0.160.1060.35428.06
7.0.150.1780.34027.93
7.0.140.1430.36727.84
7.0.130.1730.34928.18
7.0.120.1310.37428.28
7.0.110.1220.42628.13
7.0.100.0990.34727.92
7.0.90.1060.34827.87
7.0.80.1190.36328.17
7.0.70.1630.34928.18
7.0.60.3470.38028.92
7.0.50.4290.46927.88
7.0.40.1190.35229.54
7.0.30.1160.35528.33
7.0.20.2180.37028.20
7.0.10.2440.41928.24
7.0.00.2470.33829.02

preferences:
17.69 ms | 401 KiB | 5 Q