Created
February 11, 2016 08:10
-
-
Save Maikuolan/167c61f7b315ecd344c2 to your computer and use it in GitHub Desktop.
Speed test for array_merge() function of PHP.
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 | |
/** | |
* Speed test for array_merge() function of PHP. | |
* | |
* Tested using PHP 7.0.2 (2016.02.11) for Windows 7. | |
* | |
* Results are consistent, suggesting that, where the possible outputs from | |
* using "array_merge()" versus using the "+" operator aren't different, | |
* it may be faster to use the "+" operator (note that, depending on what's | |
* being merged, the outputs may sometimes differ). | |
*/ | |
$arr_test1 = array( | |
'a' => 'aaa', | |
'b' => 'bbbb', | |
'c' => 'ccccc', | |
'd' => 'dddddd', | |
'e' => 'eeeeeee', | |
'f' => 'ffffffff', | |
'g' => 'ggggggggg', | |
'h' => 'hhhhhhhhhh', | |
'i' => 'iiiiiiiiiii', | |
'j' => 'jjjjjjjjjjjj', | |
0 => 100, | |
1 => 200, | |
3 => 400, | |
4 => 800, | |
5 => 1600, | |
6 => 3200, | |
7 => 6400, | |
8 => 12800, | |
9 => 25600, | |
10 => 51200 | |
); | |
$arr_test2 = array( | |
'k' => 'kkk', | |
'l' => 'llll', | |
'm' => 'mmmmm', | |
'n' => 'nnnnnn', | |
'o' => 'ooooooo', | |
'p' => 'pppppppp', | |
'q' => 'qqqqqqqqq', | |
'r' => 'rrrrrrrrrr', | |
's' => 'sssssssssss', | |
't' => 'tttttttttttt', | |
0 => 101, | |
1 => 202, | |
3 => 404, | |
4 => 808, | |
5 => 1616, | |
6 => 3232, | |
7 => 6464, | |
8 => 12928, | |
9 => 25856, | |
10 => 51712 | |
); | |
$t = microtime(); | |
$x = array(); | |
for($i = 0; $i < 100000; $i++) { | |
$x = array_merge($arr_test1, $arr_test2); | |
} | |
echo 'Test 1: ' . (microtime() - $t) . ' seconds required.' . "\n\n"; | |
$t = microtime(); | |
$x = array(); | |
for($i = 0; $i < 100000; $i++) { | |
$x = $arr_test1 + $arr_test2; | |
} | |
echo 'Test 2: ' . (microtime() - $t) . ' seconds required.' . "\n\n"; | |
/** | |
* Results: | |
* | |
* Test 1: 0.0624 seconds required. | |
* | |
* Test 2: 0.0468 seconds required. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment