<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Troll&#039;s factory &#187; echo</title>
	<atom:link href="http://trollfactory.fr/tag/echo/feed" rel="self" type="application/rss+xml" />
	<link>http://trollfactory.fr</link>
	<description>Geekeries &#38; pensées</description>
	<lastBuildDate>Mon, 09 Jun 2014 19:43:25 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>PHP: USE concatenation NOT multiple echo / parameters</title>
		<link>http://trollfactory.fr/php-use-concatenation-not-multiple-echo-parameters-586</link>
		<comments>http://trollfactory.fr/php-use-concatenation-not-multiple-echo-parameters-586#comments</comments>
		<pubDate>Sun, 12 Aug 2012 22:21:54 +0000</pubDate>
		<dc:creator><![CDATA[Troll]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Scripts, astuces, dév. web]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[echo]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[print]]></category>

		<guid isPermaLink="false">http://trollfactory.fr/?p=586</guid>
		<description><![CDATA[Print vs. Echo I just went accross an interesting article about which of &#171;&#160;print&#160;&#187; or &#171;&#160;echo&#160;&#187; 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 [...]]]></description>
				<content:encoded><![CDATA[<h2>Print vs. Echo</h2>
<p>I just went accross an interesting article about which of &laquo;&nbsp;print&nbsp;&raquo; or &laquo;&nbsp;echo&nbsp;&raquo; we should use in PHP.</p>
<p>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.</p>
<p>Great, I believe you.</p>
<h2>Echo multiple statements / arguments vs. Concatenation</h2>
<p>But at the end of the article, the author also suggest to use <strong>multiple parameters</strong> given to one echo statement <strong>instead of concatenation because concatenation is slow&#8230;</strong> bla bla&#8230;</p>
<p>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&#8230;</p>
<p><strong>The benchmark was clear: concatenation IS FASTER</strong> than the two other solutions.</p>
<h2>String caching? Making a more relevant benchmark</h2>
<p>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 &laquo;&nbsp;maybe the PHP VM is <strong>caching</strong> the result of the concatenated string, thus the benchmark is not relevant&nbsp;&raquo;.</p>
<p>OK, let&rsquo;s check with a counter-possible-caching benchmark then:</p>
<pre>

$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! <br />";
}
$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" , "<br />";
	echo "World" , "<br />";
}
$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!" , "<br />";
}
$t2 = microtime(true);
_log("Two parameters, cached: " . ($t2-$t1) . "s\n");

$t3 = microtime(true);

_log("Total benchmark time: " . ($t3-$t0)."\n");

</pre>
<p>I generated <strong>random strings</strong> 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.</p>
<p>The benchmarked has been run on a 1.6GHz Atom processor, with the following command: </p>
<pre>sudo nice -n -19 php -q echobanchmark.php > /dev/null</pre>
<p>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).</p>
<h3>And do you know the result?</h3>
<p>Here it goes: </p>
<p>Concatenation, cached: <strong>7.4381861686707s</strong><br />
Concatenation, not cached: <strong>9.9010219573975s</strong><br />
Two echos, not cached: <strong>10.4353120327s</strong><br />
Two echos, cached: <strong>14.883654117584s</strong><br />
Two parameters, not cached: <strong>10.179102897644s</strong><br />
Two parameters, cached: <strong>11.904446840286s</strong><br />
Total benchmark time: 928.97792601585</p>
<p><strong>Conclusion: Concatenation is fast, very fast.</strong> It is rougly <strong>30% faster</strong> than using the two parameters for echo (26.9268987% exactly) and roughly <strong>30% faster</strong> than two echo statements as well (28.720999% exactly) and <strong></p>
<p>The other conclusion here is that <srtong>PHP does not seem to be caching strings very well</strong>. Both the two-parameters and two-statements echo loops are <strong>much slower when using a fresh new string and much faster when using something from the previously generated array</strong>. The difference between cached / non-cached loop for the concatenation loop is also not that impressive if we observe that my &laquo;&nbsp;not cached&nbsp;&raquo; loop is using strings of random length that can go until 200 chars whereas the &laquo;&nbsp;cached&nbsp;&raquo; one is a very short string (so the difference of 2 seconds is certainly also due to the difference in length of the strings).</p>
<p>If you have any suggestion to improve the benchmark, feel free to post a comment.</p>
Share and Enjoy:<a rel="nofollow" target="_blank"  href="http://www.printfriendly.com/print/new?url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/printfriendly.png" class="sociable-img sociable-hovers" title="Print" alt="Print" /></a><a rel="nofollow" target="_blank"  href="http://www.printfriendly.com/print/new?url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/pdf.png" class="sociable-img sociable-hovers" title="PDF" alt="PDF" /></a><a rel="nofollow" target="_blank"  href="http://twitter.com/home?status=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters%20-%20http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/twitter.png" class="sociable-img sociable-hovers" title="Twitter" alt="Twitter" /></a><a rel="nofollow" target="_blank"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;t=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/facebook.png" class="sociable-img sociable-hovers" title="Facebook" alt="Facebook" /></a><a rel="nofollow" target="_blank"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;title=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters&amp;source=The+Troll%26%23039%3Bs+factory+Geekeries+%26amp%3B+pens%C3%A9es&amp;summary=Print%20vs.%20Echo%0D%0A%0D%0AI%20just%20went%20accross%20an%20interesting%20article%20about%20which%20of%20%22print%22%20or%20%22echo%22%20we%20should%20use%20in%20PHP.%0D%0A%0D%0AThe%20author%20has%20benchmarked%20the%20two%20solutions%20%28though%20the%20data%20are%20certainly%20a%20bit%20old%29%20shows%20that%2C%20although%20there%20is%20not%20much%20diffe" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/linkedin.png" class="sociable-img sociable-hovers" title="LinkedIn" alt="LinkedIn" /></a><a rel="nofollow" target="_blank"  href="http://trollfactory.fr/feed" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/rss.png" class="sociable-img sociable-hovers" title="RSS" alt="RSS" /></a><a rel="nofollow" target="_blank"  href="http://www.wikio.fr/vote?url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/wikio.png" class="sociable-img sociable-hovers" title="Wikio FR" alt="Wikio FR" /></a><a rel="nofollow" target="_blank"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;title=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters&amp;bodytext=Print%20vs.%20Echo%0D%0A%0D%0AI%20just%20went%20accross%20an%20interesting%20article%20about%20which%20of%20%22print%22%20or%20%22echo%22%20we%20should%20use%20in%20PHP.%0D%0A%0D%0AThe%20author%20has%20benchmarked%20the%20two%20solutions%20%28though%20the%20data%20are%20certainly%20a%20bit%20old%29%20shows%20that%2C%20although%20there%20is%20not%20much%20diffe" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/digg.png" class="sociable-img sociable-hovers" title="Digg" alt="Digg" /></a><a rel="nofollow" target="_blank"  href="http://delicious.com/post?url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;title=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters&amp;notes=Print%20vs.%20Echo%0D%0A%0D%0AI%20just%20went%20accross%20an%20interesting%20article%20about%20which%20of%20%22print%22%20or%20%22echo%22%20we%20should%20use%20in%20PHP.%0D%0A%0D%0AThe%20author%20has%20benchmarked%20the%20two%20solutions%20%28though%20the%20data%20are%20certainly%20a%20bit%20old%29%20shows%20that%2C%20although%20there%20is%20not%20much%20diffe" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/delicious.png" class="sociable-img sociable-hovers" title="del.icio.us" alt="del.icio.us" /></a><a rel="nofollow" target="_blank"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;title=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters&amp;annotation=Print%20vs.%20Echo%0D%0A%0D%0AI%20just%20went%20accross%20an%20interesting%20article%20about%20which%20of%20%22print%22%20or%20%22echo%22%20we%20should%20use%20in%20PHP.%0D%0A%0D%0AThe%20author%20has%20benchmarked%20the%20two%20solutions%20%28though%20the%20data%20are%20certainly%20a%20bit%20old%29%20shows%20that%2C%20although%20there%20is%20not%20much%20diffe" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/googlebookmark.png" class="sociable-img sociable-hovers" title="Google Bookmarks" alt="Google Bookmarks" /></a><a rel="nofollow" target="_blank"  href="http://technorati.com/faves?add=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/technorati.png" class="sociable-img sociable-hovers" title="Technorati" alt="Technorati" /></a><a rel="nofollow" target="_blank"  href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/sphinn.png" class="sociable-img sociable-hovers" title="Sphinn" alt="Sphinn" /></a><a rel="nofollow" target="_blank"  href="http://www.mixx.com/submit?page_url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;title=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/mixx.png" class="sociable-img sociable-hovers" title="Mixx" alt="Mixx" /></a><a rel="nofollow" target="_blank" title="Add to favorites" href="#" onclick="AddToFavorites(); return false;"><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/addtofavorites.png" class="sociable-img sociable-hovers" title="Add to favorites" alt="Add to favorites" /></a><a rel="nofollow" target="_blank"  href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;title=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/live.png" class="sociable-img sociable-hovers" title="Live" alt="Live" /></a><a rel="nofollow" target="_blank"  href="http://www.netvibes.com/share?title=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters&amp;url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/netvibes.png" class="sociable-img sociable-hovers" title="Netvibes" alt="Netvibes" /></a><a rel="nofollow" target="_blank"  href="http://www.scoopeo.com/scoop/new?newurl=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;title=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/scoopeo.png" class="sociable-img sociable-hovers" title="Scoopeo" alt="Scoopeo" /></a><a rel="nofollow" target="_blank"  href="http://www.viadeo.com/shareit/share/?url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&title=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters&urllanguage=fr" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/viadeo.png" class="sociable-img sociable-hovers" title="viadeo FR" alt="viadeo FR" /></a><a rel="nofollow" target="_blank"  href="http://identi.ca/notice/new?status_textarea=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/identica.png" class="sociable-img sociable-hovers" title="Identi.ca" alt="Identi.ca" /></a><a rel="nofollow" target="_blank"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;t=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/myspace.png" class="sociable-img sociable-hovers" title="MySpace" alt="MySpace" /></a><a rel="nofollow" target="_blank"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;title=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/stumbleupon.png" class="sociable-img sociable-hovers" title="StumbleUpon" alt="StumbleUpon" /></a><a rel="nofollow" target="_blank"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Ftrollfactory.fr%2Fphp-use-concatenation-not-multiple-echo-parameters-586&amp;submitHeadline=PHP%3A%20USE%20concatenation%20NOT%20multiple%20echo%20%2F%20parameters&amp;submitSummary=Print%20vs.%20Echo%0D%0A%0D%0AI%20just%20went%20accross%20an%20interesting%20article%20about%20which%20of%20%22print%22%20or%20%22echo%22%20we%20should%20use%20in%20PHP.%0D%0A%0D%0AThe%20author%20has%20benchmarked%20the%20two%20solutions%20%28though%20the%20data%20are%20certainly%20a%20bit%20old%29%20shows%20that%2C%20although%20there%20is%20not%20much%20diffe&amp;submitCategory=science&amp;submitAssetType=text" ><img src="//trollfactory.fr/wp-content/plugins/sociable-30/images/default/16/yahoobuzz.png" class="sociable-img sociable-hovers" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a><br/><br/>]]></content:encoded>
			<wfw:commentRss>http://trollfactory.fr/php-use-concatenation-not-multiple-echo-parameters-586/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
