Skip to content

Instantly share code, notes, and snippets.

@aurimasniekis
Forked from Furgas/composer.json
Last active June 7, 2017 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aurimasniekis/419da57af7dd02cc5ba552b37a89a4a5 to your computer and use it in GitHub Desktop.
Save aurimasniekis/419da57af7dd02cc5ba552b37a89a4a5 to your computer and use it in GitHub Desktop.
Benchmark - array with instanceof vs. typehinted variadic argument vs. separate iterator class
{
"require": {
"phpunit/php-timer": "~1.0"
}
}
<?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);
}
function testVariadicFunction(Foo ...$foos) {
return $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;
case 'vfunction':
$foos = testVariadicFunction(...$foos);
break;
}
printf("%s\n", PHP_Timer::resourceUsage());
@aurimasniekis
Copy link
Author

Here is results

➜   php -v
PHP 7.1.5 (cli) (built: May 13 2017 13:28:23) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.5, Copyright (c) 1999-2017, by Zend Technologies
➜   php test.php array
Time: 204 ms, Memory: 96.01MB
➜   php test.php variadic
Time: 217 ms, Memory: 119.76MB
➜   php test.php iterator
Time: 293 ms, Memory: 112.01MB
➜   php test.php vfunction
Time: 110 ms, Memory: 71.76MB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment