Created
June 5, 2017 12:11
-
-
Save Furgas/0663ac47d23c89db496e71cc219b0b22 to your computer and use it in GitHub Desktop.
Benchmark - array with instanceof vs. typehinted variadic argument vs. separate iterator class
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
{ | |
"require": { | |
"phpunit/php-timer": "~1.0" | |
} | |
} |
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 | |
declare(strict_types=1); | |
include(__DIR__ . '/vendor/autoload.php'); | |
class Foo { | |
public $bar = 1; | |
} | |
class Foos implements \IteratorAggregate, \Countable { | |
private $foos; | |
public function __construct(array $foos) { | |
foreach ($foos as $foo) { | |
if (!$foo instanceof Foo) { | |
throw new \TypeError(); | |
} | |
} | |
$this->foos = $foos; | |
} | |
public function getFoos(): array { | |
return $this->foos; | |
} | |
/** | |
* @return \ArrayIterator|UserType[] | |
*/ | |
public function getIterator() { | |
return new \ArrayIterator($this->foos); | |
} | |
public function count(): int { | |
return count($this->foos); | |
} | |
} | |
function testArray(bool $use_array_map, array $foos): array { | |
foreach ($foos as $foo) { | |
if (!$foo instanceof Foo) { | |
throw new \TypeError(); | |
} | |
} | |
if ($use_array_map) { | |
$new_foos = array_map( | |
function (Foo $foo): Foo { | |
return new Foo($foo->bar + 1); | |
}, | |
$foos | |
); | |
} else { | |
$new_foos = []; | |
foreach ($foos as $foo) { | |
$new_foos[] = new Foo($foo->bar + 1); | |
} | |
} | |
return $new_foos; | |
} | |
function testVariadic(bool $use_array_map, Foo ...$foos): array { | |
if ($use_array_map) { | |
$new_foos = array_map( | |
function (Foo $foo): Foo { | |
return new Foo($foo->bar + 1); | |
}, | |
$foos | |
); | |
} else { | |
$new_foos = []; | |
foreach ($foos as $foo) { | |
$new_foos[] = new Foo($foo->bar + 1); | |
} | |
} | |
return $new_foos; | |
} | |
function testIterator(bool $use_array_map, Foos $foos): Foos { | |
if ($use_array_map) { | |
$new_foos = array_map( | |
function (Foo $foo): Foo { | |
return new Foo($foo->bar + 1); | |
}, | |
iterator_to_array($foos) | |
); | |
} else { | |
$new_foos = []; | |
foreach ($foos as $foo) { | |
$new_foos[] = new Foo($foo->bar + 1); | |
} | |
} | |
return new Foos($new_foos); | |
} | |
$foos = []; | |
for ($i = 0; $i < 500000; $i++) { | |
$foos[] = new Foo(); | |
} | |
$test = $argv[1] ?? 'array'; | |
$use_array_map = isset($argv[2]) ? true : false; | |
switch ($test) { | |
case 'array': | |
$foos = testArray($use_array_map, $foos); | |
break; | |
case 'variadic': | |
$foos = testVariadic($use_array_map, ...$foos); | |
break; | |
case 'iterator': | |
$foos = testIterator($use_array_map, new Foos($foos)); | |
break; | |
} | |
printf("%s\n", PHP_Timer::resourceUsage()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results with foreach:
Result with array_map: