<?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; premature optimization</title>
	<atom:link href="http://www.varslashlog.com/tag/premature-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>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>
