<?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>var/log &#187; Optimization</title>
	<atom:link href="http://www.varslashlog.com/category/programming/optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.varslashlog.com</link>
	<description>Yet another weblog</description>
	<lastBuildDate>Sat, 12 Sep 2009 13:34:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Busting &#8220;echo is faster than print&#8221;</title>
		<link>http://www.varslashlog.com/2008/11/05/busting-echo-is-faster-than-print/</link>
		<comments>http://www.varslashlog.com/2008/11/05/busting-echo-is-faster-than-print/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 23:17:09 +0000</pubDate>
		<dc:creator>AHSauge</dc:creator>
				<category><![CDATA[Optimization]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[echo]]></category>
		<category><![CDATA[echo or print]]></category>
		<category><![CDATA[echo vs print]]></category>
		<category><![CDATA[print]]></category>

		<guid isPermaLink="false">http://www.varslashlog.com/?p=89</guid>
		<description><![CDATA[I&#8217;ve previously been talking about premature optimization. Today I thought I should illustrate how ridicules some of the stuff is by busting &#8220;echo is faster than print&#8221;. There&#8217;s a lot of people calming that this is true (1, 2, 3, 4 and a lot of others). Now to be clear, they are somewhat right about [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve previously been talking about <a title="Premature optimization is bad" href="http://www.varslashlog.com/2008/11/01/premature-optimization-is-bad/" target="_blank">premature optimization</a>. Today I thought I should illustrate how ridicules some of the stuff is by busting &#8220;echo is faster than print&#8221;. There&#8217;s a lot of people calming that this is true (<a href="http://elliottback.com/wp/php-performance-echo-print/trackback/" target="_blank">1</a>, <a href="http://reinholdweber.com/?p=3" target="_blank">2</a>, <a href="http://www.chazzuka.com/blog/wp-trackback.php?p=163" target="_blank">3</a>, <a href="http://hmvrulz.wordpress.com/2008/09/23/20-php-optimization-tips-make-it-faster/" target="_blank">4</a> and <a title="Search result for 'echo is faster than print'" href="http://www.google.no/search?q=echo+is+faster+than+print" target="_blank">a lot of others</a>). Now to be clear, they are somewhat right about this. Echo <em>is</em> really faster than print, which it really should due to the fact that echo don&#8217;t return anything while print does. The problem and my point here however, is how much this difference really is. The answer is so tiny that you can forget actually measuring it in a real world application. There&#8217;s why:<span id="more-89"></span><br />
First off, the code I&#8217;ve been using is</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_fill</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$end</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$res</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$end</span><span style="color: #339933;">-</span><span style="color: #000088;">$start</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">ob_end_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$res</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_fill</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$end</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$res</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$end</span><span style="color: #339933;">-</span><span style="color: #000088;">$start</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">ob_end_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$res</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As you can see I&#8217;ve tried to make this as simple as possible. I&#8217;ve used output buffer to remove the huge variables also known as browser and network (yes, they&#8217;re really a bottleneck here, trust me or try comparing firefox with lynx). Testing was done in the 64bit version of Fedora 9 (which is a linux distro) with PHP 5.2.6 on an AMD Athlon 64 X2 4600 (dual core CPU running at standard 2.4GHz).</p>
<p>Running this gives me an average of 0.333 seconds for echo and 0.346 seconds for print. Doing some calculation you&#8217;ll see that the average difference between each echo and print is 13ns or 0.000000013 seconds. Even though this is a 3.8% difference, it&#8217;s not even a millionth of a second, but 13 billionth! Hardly any difference if you ask me. I know, it&#8217;s just testing with a single, silly punctuation mark. To prevent people from going &#8220;There must be a bigger difference with a longer string!&#8221;, I ran the test with the first paragraph of lorem ipsum.</p>
<blockquote><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc luctus arcu vehicula est. Donec facilisis iaculis magna. Mauris neque dui, varius in, fermentum id, scelerisque et, massa. Nam ac velit nec odio molestie pellentesque. Nulla dolor mauris, tempus ultrices, cursus in, ultrices sollicitudin, orci. Sed at ligula. Sed id erat id nisl molestie tempus. Vestibulum nibh dolor, vulputate nec, dictum non, sollicitudin nec, nibh. Sed vitae diam eget felis dignissim tempor. Aenean vel risus. Integer consectetuer nibh. Ut eu nunc. Donec at sapien.</p>
</blockquote>
<p>This is 551 characters, which should satisfy most people. Results? 0.987 seconds for echo and 1.014 seconds for print, meaning echo is 2.6% faster. Again, hardly any difference as this is 27ns or 0.000000027 seconds. Conclusion: Myth busted! You&#8217;ll have to do a ridicules amount of outputing to make any actual difference.</p>
<p>PS: If you run this on your own computer, I would love to hear about your results (including type of CPU, OS and PHP version). Remember this though: Please bar in mind that you&#8217;ll have to disable any CPU-throttling and run it in separate files to get accurate results.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.varslashlog.com/2008/11/05/busting-echo-is-faster-than-print/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Premature optimization is bad</title>
		<link>http://www.varslashlog.com/2008/11/01/premature-optimization-is-bad/</link>
		<comments>http://www.varslashlog.com/2008/11/01/premature-optimization-is-bad/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 00:45:15 +0000</pubDate>
		<dc:creator>AHSauge</dc:creator>
				<category><![CDATA[Optimization]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[premature optimization]]></category>

		<guid isPermaLink="false">http://www.varslashlog.com/?p=76</guid>
		<description><![CDATA[Donald Knuth once wrote that &#8220;premature optimization is the root of all evil&#8221;. I have to agree with him on that. One thing I&#8217;ve been dying to write about is the massive amount of crappy PHP optimization tips found on the net. Two examples of such is Reinhold Weber&#8217;s 40 Tips for optimizing your php [...]]]></description>
			<content:encoded><![CDATA[<p>Donald Knuth once wrote that &#8220;premature optimization is the root of all evil&#8221;. I have to agree with him on that. One thing I&#8217;ve been dying to write about is the massive amount of crappy PHP optimization tips found on the net. Two examples of such is Reinhold Weber&#8217;s <a title="40 Tips for optimizing your php code" href="http://reinholdweber.com/?p=3" target="_blank">40 Tips for optimizing your php code</a> and chazzuka&#8217;s <a href="http://www.chazzuka.com/blog/?p=163" target="_blank">63+ best practice to optimize PHP code performances</a>. They <em>do</em> both have some good tips here and there, but in general this is just stupid tips, or more precise, premature optimization. There are reasons why such tips are bad.<span id="more-76"></span></p>
<p>First of all, most of these tips are undocumented and presented as obvious facts. This makes them hard to disprove or confirm as the reality is a bit different and more complicated than what these people tend to think. A good example of such a clam is &#8220;a foreach-loop is faster than a for-loop&#8221; or vica versa. I&#8217;ve tested this a couple of times, and though I get a consistent result in favor of for-loop, just changing the platform from Linux to Windows seems to reverse the result. This displays a major problem. Even the slightest difference in OS, software version, hardware etc. can change the results significantly. This is never mentioned by people presenting optimization &#8220;tips&#8221;.</p>
<p>Second, even though the result is correct, it&#8217;s often pointless to actually make the change because you&#8217;ll never actually save considerable amount of time. Let&#8217;s look at the first point in both my links.</p>
<blockquote><p>If a method can be static, declare it static. Speed improvement is by a factor of 4.</p></blockquote>
<p>Sounds good, doesn&#8217;t it? Speed improvements by a factor of 4! That must be significant, right? No, not necessarily. It can very well be an improvement by a factor of 4 (even though my testing seem to show about equal speed), but that&#8217;s only as significant as the time it initially takes. If one execution takes, say 1µs, saving 0.75µs isn&#8217;t that significant (µs = microsecond = 10^-6 s = 0.000001) considering the function itself might using 100µs on each execution. This is very often the case when these people present their &#8220;tips&#8221; if they&#8217;re actually correct on what&#8217;s the faster part.</p>
<p>Third, with the previous stuff in mind. Even if the clam is correct and even if there is some performance to be gained, is it worth it? Not necessarily. There are several things to keep in mind here. Primarily optimization degrades readability, and there&#8217;s also the issue of someone actually have to make these changes. Is it really that worth it if you have to spend an hour changing alot of code just to have a cumulative saving of about 1ms? I don&#8217;t think so &#8230;</p>
<p>The final nail in the coffin for these &#8220;tips&#8221; is the fact that it&#8217;s never ever tested with any load. Load is important as that&#8217;s the reality. It doesn&#8217;t matter if solution A is x times faster than solution B saving y amount of seconds, if solution A hits a bottleneck when it&#8217;s run in 10 parallel requests making it much slower than solution B under the same conditions. Here&#8217;s where real optimization come in handy. Caching stuff, using a PHP accelerator, having a thought through algorithm, actually knowing what&#8217;s the perfomance hit and so on is always going to beat these type of lists hands down anytime. That&#8217;s the reality of optimization, not whether or not you use single or double quotes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.varslashlog.com/2008/11/01/premature-optimization-is-bad/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
