The Troll's factory

Geekeries & pensées
-->

Archive pour la catégorie "PHP"

PHP: USE concatenation NOT multiple echo / parameters

Print vs. Echo

I just went accross an interesting article about which of « print » or « echo » we should use in PHP.

The author has benchmarked the two solutions (though the data are certainly a bit old) shows that, although there is not much difference between the two, echo seems to be more efficient.

Great, I believe you.

Echo multiple statements / arguments vs. Concatenation

But at the end of the article, the author also suggest to use multiple parameters given to one echo statement instead of concatenation because concatenation is slow… bla bla…

As I have always been using concatenation (did not even know echo could take multiple arguments) and I never had any performance issue, I decided to check that and make a benchmark: echo 100,000 times two concatenated strings and do the same thing with two parameters and two echos statements…

The benchmark was clear: concatenation IS FASTER than the two other solutions.

String caching? Making a more relevant benchmark

As it appeared a bit weird to me, having been doing Java / C# mostly recently (with their slow concatenation that recreated a new object blabla), I thought « maybe the PHP VM is caching the result of the concatenated string, thus the benchmark is not relevant ».

OK, let’s check with a counter-possible-caching benchmark then:


$t0 = microtime(true);

define("MAX", 2000000);
File_put_ConTents("echobench._log", "");
function _log($str)
{
	File_put_ConTents("echobench._log", file_get_contents("echobench._log") . $str);
}

// Generating a bunch of string to concatenate to avoid PHP VM-caching
$strs = array();
for ($i=0; $i < MAX+1; $i++) { 
	$m = mt_rand(0, 200);
	$strs[$i] = '';
	for ($j=0; $j < $m; $j++) { 
		$strs[$i] .= chr(mt_rand(0, 255));
	}
}

// Loop where string concatenation caching is possible :
$t1 = microtime(true);
for ($i=0; $i < MAX; $i++) { 
	echo "Hello" . "World! 
"; } $t2 = microtime(true); _log("Concatenation, cached: " . ($t2-$t1) . "s\n"); // Loop where is is not : $t1 = microtime(true); for ($i=0; $i < MAX; $i++) { echo $strs[$i] . $strs[$i + 1]; } $t2 = microtime(true); _log("Concatenation, not cached: " . ($t2-$t1) . "s\n"); // Other ways to display stuff, with and without caching possible each time: $t1 = microtime(true); for ($i=0; $i < MAX; $i++) { echo $strs[$i]; echo $strs[$i + 1]; } $t2 = microtime(true); _log("Two echos, not cached: " . ($t2-$t1) . "s\n"); $t1 = microtime(true); for ($i=0; $i < MAX; $i++) { echo "Hello" , "
"; echo "World" , "
"; } $t2 = microtime(true); _log("Two echos, cached: " . ($t2-$t1) . "s\n"); $t1 = microtime(true); for ($i=0; $i < MAX; $i++) { echo $strs[$i], $strs[$i + 1];; } $t2 = microtime(true); _log("Two parameters, not cached: " . ($t2-$t1) . "s\n"); $t1 = microtime(true); for ($i=0; $i < MAX; $i++) { echo "Hello" , "World!" , "
"; } $t2 = microtime(true); _log("Two parameters, cached: " . ($t2-$t1) . "s\n"); $t3 = microtime(true); _log("Total benchmark time: " . ($t3-$t0)."\n");

I generated random strings at the beginning of the script. And I concatenate them after, in the loop. Thus, there is not even once the same concatenation happening, thus, no possible caching or whatever.

The benchmarked has been run on a 1.6GHz Atom processor, with the following command:

sudo nice -n -19 php -q echobanchmark.php > /dev/null

So the process was the most prioritized one on the OS and then there is no reason for any interference between the loops (moreover, the system was idle during the test (and there are 3 other virtual processors for doing whatever work the OS would need to do).

And do you know the result?

Here it goes:

Concatenation, cached: 7.4381861686707s
Concatenation, not cached: 9.9010219573975s
Two echos, not cached: 10.4353120327s
Two echos, cached: 14.883654117584s
Two parameters, not cached: 10.179102897644s
Two parameters, cached: 11.904446840286s
Total benchmark time: 928.97792601585

Conclusion: Concatenation is fast, very fast. It is rougly 30% faster than using the two parameters for echo (26.9268987% exactly) and roughly 30% faster than two echo statements as well (28.720999% exactly) and

The other conclusion here is that PHP does not seem to be caching strings very well. Both the two-parameters and two-statements echo loops are much slower when using a fresh new string and much faster when using something from the previously generated array. The difference between cached / non-cached loop for the concatenation loop is also not that impressive if we observe that my « not cached » loop is using strings of random length that can go until 200 chars whereas the « cached » one is a very short string (so the difference of 2 seconds is certainly also due to the difference in length of the strings).

If you have any suggestion to improve the benchmark, feel free to post a comment.

posté par Troll dans PHP,Scripts, astuces, dév. web avec aucun commentaire