Created
October 12, 2015 21:03
-
-
Save tacone/6abbfa862aa77806a77d to your computer and use it in GitHub Desktop.
Arrays and iterators benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Aggregate implements \IteratorAggregate | |
{ | |
protected $var; | |
public function __construct($var = null) | |
{ | |
if (is_array($var)) { | |
$this->var = new ArrayIterator($var); | |
} | |
if ($var instanceof \Traversable) { | |
$this->var = $var; | |
} | |
} | |
public function next() | |
{ | |
echo 'x'; | |
} | |
public function toArray() | |
{ | |
return iterator_to_array($this->var, true); | |
} | |
public function getIterator() | |
{ | |
return $this->var; | |
} | |
} | |
class Outer implements \OuterIterator | |
{ | |
protected $var; | |
public function __construct($var = null) | |
{ | |
if (is_array($var)) { | |
$this->var = new ArrayIterator($var); | |
} | |
if ($var instanceof \Traversable) { | |
$this->var = $var; | |
} | |
} | |
public function toArray() | |
{ | |
return iterator_to_array($this->var, true); | |
} | |
public function getInnerIterator() | |
{ | |
return $this->var; | |
} | |
public function current() | |
{ | |
return $this->var->current(); | |
} | |
public function next() | |
{ | |
$this->var->next(); | |
} | |
public function key() | |
{ | |
return $this->var->key(); | |
} | |
public function valid() | |
{ | |
return $this->var->valid(); | |
} | |
public function rewind() | |
{ | |
$this->var->rewind(); | |
} | |
} | |
class IteratorIt extends IteratorIterator | |
{ | |
public function __construct($var = null) | |
{ | |
if (is_array($var)) { | |
$var = new ArrayIterator($var); | |
} | |
parent::__construct($var); | |
} | |
public function toArray() | |
{ | |
return iterator_to_array($this->getInnerIterator(), true); | |
} | |
public function getIterator() | |
{ | |
return $this->getInnerIterator(); | |
} | |
} | |
function generator($var) | |
{ | |
foreach ($var as $key => $val) { | |
yield $key => $val; | |
} | |
return; | |
} | |
function bench($name, $test) | |
{ | |
echo "$name: "; | |
$start = microtime(true); | |
$test(); | |
$time = microtime(true); | |
$time -= $start; | |
echo ($time * 1000) . ' ms' . PHP_EOL; | |
} | |
$max = 1e6; | |
$array = range(1, 1e6); | |
//$array = (new SplFixedArray(1e6))->fromArray($array); | |
//$array = new ArrayIterator($array); | |
$testSuites = [ | |
'Outer_ToArray' => function () use ($max, $array) { | |
$iterator = new Outer($array); | |
$r = $iterator->toArray(); | |
}, | |
'Aggregate_ToArray' => function () use ($max, $array) { | |
$iterator = new Aggregate($array); | |
$r = $iterator->toArray(); | |
}, | |
'IteratorIt_ToArray' => function () use ($max, $array) { | |
$iterator = new IteratorIt($array); | |
$r = $iterator->toArray(); | |
}, | |
'Generator_ToArray' => function () use ($max, $array) { | |
$iterator = generator($array); | |
$r = iterator_to_array($iterator); | |
}, | |
'Outer_Foreach' => function () use ($max, $array) { | |
$iterator = new Outer($array); | |
foreach ($iterator as $k => $v) { | |
} | |
}, | |
'IteratorIt_Foreach' => function () use ($max, $array) { | |
$iterator = new IteratorIt($array); | |
foreach ($iterator as $k => $v) { | |
} | |
}, | |
'Aggregate_Foreach' => function () use ($max, $array) { | |
$iterator = new Aggregate($array); | |
foreach ($iterator as $k => $v) { | |
} | |
}, | |
'Generator_Foreach' => function () use ($max, $array) { | |
$iterator = generator($array); | |
foreach ($iterator as $k => $v) { | |
} | |
}, | |
'Array_Foreach' => function () use ($max, $array) { | |
foreach ($array as $k => $v) { | |
} | |
}, | |
]; | |
echo '------------------------------------' . PHP_EOL; | |
foreach ($testSuites as $name => $test) { | |
bench($name, $test); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment