Last active
February 11, 2019 14:43
-
-
Save Ocramius/8399625 to your computer and use it in GitHub Desktop.
Simple test that compares `array_merge` and `foreach` while merging arrays by overwriting keys
This file contains hidden or 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
tests | |
composer.lock | |
composer.phar | |
vendor |
This file contains hidden or 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 | |
namespace ArrayTest; | |
use Athletic\AthleticEvent; | |
class ArrayMergeEvent extends AthleticEvent | |
{ | |
private $bigMap1 = []; | |
private $bigMap2 = []; | |
private $bigMap3 = []; | |
private $chunkLength = 1000; | |
public function setUp() | |
{ | |
for ($mapIdx = 1; $mapIdx < 4; $mapIdx += 1) { | |
for ($i = 0; $i < $this->chunkLength; $i += 1) { | |
$this->{'bigMap' . $mapIdx}['index' . $i] = $i; | |
} | |
} | |
} | |
/** | |
* @baseline | |
* @iterations 1000 | |
* @group classmaps-performance | |
* | |
* @return mixed | |
*/ | |
public function singleArrayMerge() | |
{ | |
$map = []; | |
$map = array_merge($map, $this->bigMap1, $this->bigMap2, $this->bigMap3); | |
return $map; | |
} | |
/** | |
* @iterations 1000 | |
* @group classmaps-performance | |
* | |
* @return mixed | |
*/ | |
public function singleArrayMergeCallUserFuncArray() | |
{ | |
$map = []; | |
$map = call_user_func_array('array_merge', [$map, $this->bigMap1, $this->bigMap2, $this->bigMap3]); | |
return $map; | |
} | |
/** | |
* @iterations 1000 | |
* @group classmaps-performance | |
* | |
* @return mixed | |
*/ | |
public function arrayMergePerformance() | |
{ | |
$map = []; | |
$map = array_merge($map, $this->bigMap1); | |
$map = array_merge($map, $this->bigMap2); | |
$map = array_merge($map, $this->bigMap3); | |
return $map; | |
} | |
/** | |
* @iterations 1000 | |
* @group classmaps-performance | |
* | |
* @return mixed | |
*/ | |
public function arraySumPerformance() | |
{ | |
$map = []; | |
$map = $map + $this->bigMap1; | |
$map = $map + $this->bigMap2; | |
$map = $map + $this->bigMap3; | |
return $map; | |
} | |
/** | |
* @iterations 1000 | |
* @group classmaps-performance | |
* | |
* @return mixed | |
*/ | |
public function singleArraySumPerformance() | |
{ | |
$map = []; | |
$map = $map + $this->bigMap1 + $this->bigMap2 + $this->bigMap3; | |
return $map; | |
} | |
/** | |
* @iterations 1000 | |
* @group classmaps-performance | |
* | |
* @return mixed | |
*/ | |
public function arrayReplacePerformance() | |
{ | |
$map = []; | |
$map = array_replace($map, $this->bigMap1); | |
$map = array_replace($map, $this->bigMap2); | |
$map = array_replace($map, $this->bigMap3); | |
return $map; | |
} | |
/** | |
* @iterations 1000 | |
* @group classmaps-performance | |
* | |
* @return mixed | |
*/ | |
public function singleArrayReplacePerformance() | |
{ | |
$map = []; | |
$map = array_replace($map, $this->bigMap1, $this->bigMap2, $this->bigMap3); | |
return $map; | |
} | |
/** | |
* @iterations 1000 | |
* @group classmaps-performance | |
* | |
* @return mixed | |
*/ | |
public function singleArrayReplaceCallUserFuncArray() | |
{ | |
$map = []; | |
$map = call_user_func_array('array_replace', [$map, $this->bigMap1, $this->bigMap2, $this->bigMap3]); | |
return $map; | |
} | |
/** | |
* @iterations 1000 | |
* @group classmaps-performance | |
* | |
* @return mixed | |
*/ | |
public function forEachPerformance() | |
{ | |
$map = []; | |
foreach ($this->bigMap1 as $k => $v) { | |
$map[$k] = $v; | |
} | |
foreach ($this->bigMap2 as $k => $v) { | |
$map[$k] = $v; | |
} | |
foreach ($this->bigMap3 as $k => $v) { | |
$map[$k] = $v; | |
} | |
return $map; | |
} | |
} |
This file contains hidden or 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": { | |
"php": ">=5.4", | |
"athletic/athletic": "~0.1.7" | |
}, | |
"autoload": { | |
"classmap": [ | |
"tests/" | |
] | |
} | |
} |
This file contains hidden or 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 5.4.17 (cli) (built: Sep 12 2013 23:14:23) | |
Copyright (c) 1997-2013 The PHP Group | |
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies | |
ArrayTest\ArrayMergeEvent | |
classmaps-performance | |
Method Name Iterations Average Time Ops/s Relative | |
------------------------------ ---------- ------------ -------------- --------- --------- | |
singleArrayMerge : [Baseline] [1,000 ] [0.0003296041489] [3,033.94239] | |
singleArrayMergeCallUserFuncArray: [1,000 ] [0.0003857252598] [2,592.51883] [117.03%] | |
arrayMergePerformance : [1,000 ] [0.0006753828526] [1,480.64168] [204.91%] | |
arraySumPerformance : [1,000 ] [0.0002398200035] [4,169.79395] [72.76%] | |
singleArraySumPerformance : [1,000 ] [0.0001998865604] [5,002.83760] [60.64%] | |
arrayReplacePerformance : [1,000 ] [0.0004743635654] [2,108.08770] [143.92%] | |
singleArrayReplacePerformance : [1,000 ] [0.0002584493160] [3,869.23059] [78.41%] | |
singleArrayReplaceCallUserFuncArray: [1,000 ] [0.0002647943497] [3,776.51563] [80.34%] | |
forEachPerformance : [1,000 ] [0.0003919577599] [2,551.29532] [118.92%] | |
real 0m14.217s | |
user 0m14.202s | |
sys 0m0.013s | |
========================================= | |
PHP 5.5.7 (cli) (built: Dec 19 2013 04:11:25) | |
Copyright (c) 1997-2013 The PHP Group | |
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies | |
with Zend OPcache v7.0.2, Copyright (c) 1999-2013, by Zend Technologies | |
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans | |
ArrayTest\ArrayMergeEvent | |
classmaps-performance | |
Method Name Iterations Average Time Ops/s Relative | |
------------------------------ ---------- ------------ -------------- --------- --------- | |
singleArrayMerge : [Baseline] [1,000 ] [0.0001293578148] [7,730.49546] | |
singleArrayMergeCallUserFuncArray: [1,000 ] [0.0001246533394] [8,022.24798] [96.36%] | |
arrayMergePerformance : [1,000 ] [0.0002421107292] [4,130.34153] [187.16%] | |
arraySumPerformance : [1,000 ] [0.0001546700001] [6,465.37790] [119.57%] | |
singleArraySumPerformance : [1,000 ] [0.0001556797028] [6,423.44495] [120.35%] | |
arrayReplacePerformance : [1,000 ] [0.0001710965633] [5,844.65275] [132.27%] | |
singleArrayReplacePerformance : [1,000 ] [0.0000889427662] [11,243.18528] [68.76%] | |
singleArrayReplaceCallUserFuncArray: [1,000 ] [0.0000894100666] [11,184.42294] [69.12%] | |
forEachPerformance : [1,000 ] [0.0003338775635] [2,995.10991] [258.10%] | |
real 0m9.971s | |
user 0m9.959s | |
sys 0m0.012s | |
========================================= | |
HipHop VM v2.3.2 (rel) | |
Compiler: 1388913189_N | |
Repo schema: N_1388913189 | |
ArrayTest\ArrayMergeEvent | |
classmaps-performance | |
Method Name Iterations Average Time Ops/s Relative | |
------------------------------ ---------- ------------ -------------- --------- --------- | |
singleArrayMerge : [Baseline] [1,000 ] [0.0000611436378] [16,354.93166] | |
singleArrayMergeCallUserFuncArray: [1,000 ] [0.0000586435805] [17,052.16511] [95.91%] | |
arrayMergePerformance : [1,000 ] [0.0000904490964] [11,055.94260] [147.93%] | |
arraySumPerformance : [1,000 ] [0.0000495669851] [20,174.71946] [81.07%] | |
singleArraySumPerformance : [1,000 ] [0.0000501480112] [19,940.97064] [82.02%] | |
arrayReplacePerformance : [1,000 ] [0.0001812064679] [5,518.56691] [296.36%] | |
singleArrayReplacePerformance : [1,000 ] [0.0000978982466] [10,214.68772] [160.11%] | |
singleArrayReplaceCallUserFuncArray: [1,000 ] [0.0000989193933] [10,109.24131] [161.78%] | |
forEachPerformance : [1,000 ] [0.0002228856125] [4,486.60641] [364.53%] | |
real 0m9.207s | |
user 0m8.618s | |
sys 0m0.032s | |
========================================= | |
HHVM with JIT: | |
HipHop VM v2.3.3 (rel) | |
Compiler: tags/HHVM-2.3.3-1-g5d9b794c3a08e729764b03dc871a8be5603d2f9f | |
Repo schema: 9fbd625b9fdbef8c0da8a8d89178599bbf4b1a40 | |
ArrayTest\ArrayMergeEvent | |
classmaps-performance | |
Method Name Iterations Average Time Ops/s Relative | |
------------------------------ ---------- ------------ -------------- --------- --------- | |
singleArrayMerge : [Baseline] [1,000 ] [0.0000593159209] [16,858.88042] | |
singleArrayMergeCallUserFuncArray: [1,000 ] [0.0000590510378] [16,934.50367] [99.55%] | |
arrayMergePerformance : [1,000 ] [0.0000905821339] [11,039.70479] [152.71%] | |
arraySumPerformance : [1,000 ] [0.0000361342436] [27,674.58003] [60.92%] | |
singleArraySumPerformance : [1,000 ] [0.0000277793412] [35,997.97451] [46.83%] | |
arrayReplacePerformance : [1,000 ] [0.0001772999794] [5,640.15868] [298.91%] | |
singleArrayReplacePerformance : [1,000 ] [0.0000999188440] [10,008.12236] [168.45%] | |
singleArrayReplaceCallUserFuncArray: [1,000 ] [0.0001034808177] [9,663.62694] [174.46%] | |
forEachPerformance : [1,000 ] [0.0000988578813] [10,115.53155] [166.66%] | |
real 0m3.818s | |
user 0m3.800s | |
sys 0m0.012s |
This file contains hidden or 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
#/bin/sh | |
curl -s https://getcomposer.org/installer | php | |
mkdir tests | |
cp ArrayMergeEvent.php tests/ | |
php composer.phar install | |
time php -n ./vendor/bin/athletic -p ./tests/ -b ./vendor/autoload.php -f GroupedFormatter |
@jeremyquinton experimenting for zendframework/zendframework#5716 ;)
I don't think you can use call_user_func_array
to benchmark a function, this function itself will impact performances a lot.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Couldn't understand how you were doing the benchmarks but then realised you were using https://github.com/polyfractal/athletic which took me 2 minutes to work out so adding that here for anyone else who reads this but doesn't know about athletic