Last active
October 22, 2019 10:39
-
-
Save andrey-legayev/24ed101a34d4775bf1b6f9879581f51a to your computer and use it in GitHub Desktop.
PHP benchmark: String concatenation vs. Array join()
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 | |
function str_concat($size) | |
{ | |
$result = ""; | |
for ($i = 0; $i < $size; $i++) { | |
$result .= (string)rand() . "\n"; | |
} | |
return $result; | |
} | |
function array_join($size) | |
{ | |
$result = []; | |
for ($i = 0; $i < $size; $i++) { | |
$result [] = (string)rand() . "\n"; | |
} | |
return join('', $result); | |
} | |
function array_join2($size) | |
{ | |
$result = []; | |
for ($i = 0; $i < $size; $i++) { | |
$result [] = (string)rand(); | |
} | |
return join("\n", $result); | |
} | |
function test($size, $iterations) | |
{ | |
// run tests | |
$t = microtime(true); | |
for ($j = 0; $j < $iterations; $j++) { | |
$x = str_concat($size); | |
} | |
$t1 = microtime(true) - $t; | |
$t = microtime(true); | |
for ($j = 0; $j < $iterations; $j++) { | |
$x = array_join($size); | |
} | |
$t2 = microtime(true) - $t; | |
$t = microtime(true); | |
for ($j = 0; $j < $iterations; $j++) { | |
$x = array_join2($size); | |
} | |
$t3 = microtime(true) - $t; | |
// echo results as CSV | |
printf("%s, %d, %d, %f, %f, %f\n", phpversion(), $size, $iterations, $t1, $t2, $t3); | |
} | |
echo "# PHP Version, Array Size, Iterations, str_concat, array_join, array_join2\n"; | |
for ($i = 1; $i <= 100000; $i = $i * 10) { | |
test($i, 1000); | |
} |
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
#!/bin/bash | |
for ver in 5.6 7.0 7.1 7.2 7.3; do | |
echo "# running tests for version $ver" | |
image=php:$ver-cli | |
docker run --rm -v "$PWD":/src $image \ | |
bash -c "php /src/perf-test.php" | |
done |
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
# running tests for version 5.6 | |
# PHP Version, Parts Count, Iterations, str_concat, array_join, array_join2, array_join3, array_join4 | |
5.6.40, 1, 1000, 0.001597, 0.001300, 0.001112, 0.001087, 0.001468 | |
5.6.40, 10, 1000, 0.005058, 0.007327, 0.006817, 0.008745, 0.010718 | |
5.6.40, 100, 1000, 0.057380, 0.061603, 0.070954, 0.058866, 0.086805 | |
5.6.40, 1000, 1000, 0.515179, 0.626859, 0.657277, 0.809347, 1.323092 | |
5.6.40, 10000, 1000, 5.058190, 7.374991, 7.665942, 6.673272, 11.688725 | |
5.6.40, 100000, 1000, 52.133853, 71.907317, 76.902235, 67.298046, 111.846944 | |
# running tests for version 7.0 | |
# PHP Version, Parts Count, Iterations, str_concat, array_join, array_join2, array_join3, array_join4 | |
7.0.33, 1, 1000, 0.000739, 0.000957, 0.000787, 0.000645, 0.000744 | |
7.0.33, 10, 1000, 0.003980, 0.003915, 0.006549, 0.003564, 0.004391 | |
7.0.33, 100, 1000, 0.061051, 0.033678, 0.037254, 0.029608, 0.044159 | |
7.0.33, 1000, 1000, 0.336042, 0.332554, 0.373780, 0.291184, 0.342935 | |
7.0.33, 10000, 1000, 3.231953, 3.307821, 3.915566, 2.909325, 4.061225 | |
7.0.33, 100000, 1000, 31.976340, 35.209253, 43.898724, 30.411636, 37.618492 | |
# running tests for version 7.1 | |
# PHP Version, Parts Count, Iterations, str_concat, array_join, array_join2, array_join3, array_join4 | |
7.1.32, 1, 1000, 0.000717, 0.000851, 0.000796, 0.000810, 0.000885 | |
7.1.32, 10, 1000, 0.004571, 0.005157, 0.005320, 0.004284, 0.005708 | |
7.1.32, 100, 1000, 0.046951, 0.043843, 0.042463, 0.032842, 0.033185 | |
7.1.32, 1000, 1000, 0.316417, 0.314221, 0.382685, 0.271413, 0.330734 | |
7.1.32, 10000, 1000, 3.605251, 2.855439, 3.496263, 2.446178, 3.149273 | |
7.1.32, 100000, 1000, 28.017887, 30.284794, 38.747616, 26.073231, 32.535485 | |
# running tests for version 7.2 | |
# PHP Version, Parts Count, Iterations, str_concat, array_join, array_join2, array_join3, array_join4 | |
7.2.23, 1, 1000, 0.000788, 0.000700, 0.001279, 0.000593, 0.000684 | |
7.2.23, 10, 1000, 0.003656, 0.003471, 0.004541, 0.003646, 0.004430 | |
7.2.23, 100, 1000, 0.044852, 0.027060, 0.035542, 0.023614, 0.030209 | |
7.2.23, 1000, 1000, 0.274020, 0.297881, 0.327138, 0.237992, 0.282198 | |
7.2.23, 10000, 1000, 2.742886, 2.812583, 3.303890, 2.627340, 3.123135 | |
7.2.23, 100000, 1000, 27.193778, 31.518518, 40.634590, 27.752904, 37.442942 | |
# running tests for version 7.3 | |
# PHP Version, Parts Count, Iterations, str_concat, array_join, array_join2, array_join3, array_join4 | |
7.3.10, 1, 1000, 0.000495, 0.000600, 0.000685, 0.000576, 0.000687 | |
7.3.10, 10, 1000, 0.003745, 0.003689, 0.006031, 0.003448, 0.004353 | |
7.3.10, 100, 1000, 0.046929, 0.030393, 0.027951, 0.024449, 0.030039 | |
7.3.10, 1000, 1000, 0.290486, 0.279173, 0.323931, 0.244155, 0.319280 | |
7.3.10, 10000, 1000, 2.696267, 2.776577, 3.294820, 2.355970, 3.226814 | |
7.3.10, 100000, 1000, 26.631122, 30.527062, 42.366897, 27.234378, 36.725523 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As you can see there is no real way to optimize massive string concatenation in PHP :-\