<?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>Phil Wallach &#187; sydphp</title>
	<atom:link href="http://philwallach.com/tag/sydphp/feed/" rel="self" type="application/rss+xml" />
	<link>http://philwallach.com</link>
	<description>I follow where my mind leads ...</description>
	<lastBuildDate>Wed, 17 Jun 2009 03:39:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP profiling: comparisons</title>
		<link>http://philwallach.com/2009/06/17/php-profiling-comparisons/</link>
		<comments>http://philwallach.com/2009/06/17/php-profiling-comparisons/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 03:11:39 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[sydphp]]></category>

		<guid isPermaLink="false">http://philwallach.com/?p=97</guid>
		<description><![CDATA[An application I created used a lot of MySQL ENUMS.  They are kind of useful, in that the value of an ENUM column does not need a lookup.  The downside is that it lead to a lot of string comparisons in the code, and this became a prime suspect when performance degraded. However a colleague [...]]]></description>
			<content:encoded><![CDATA[<p>An application I created used a lot of MySQL ENUMS.  They are kind of useful, in that the value of an ENUM column does not need a lookup.  The downside is that it lead to a lot of string comparisons in the code, and this became a prime suspect when performance degraded.</p>
<p>However a colleague of mine suggested that it may be due to the cost of pass-by-value as opposed to pass-by reference.</p>
<p>So I wanted to sort it out and this simple test is the result.  After all, the first rule of profiling is &#8220;measure, measure, measure&#8221; (OK, the first three rules).  The following code compares string comparisons as pass-by-value, pass-by-reference and inline, as well as an integer (constant) comparison.</p>
<p>The profiling code:<br />
<pre><code>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function compare_value($str)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($str == &#039;MekTek&#039;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function compare_ref(&amp;$str)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($str == &#039;MekTek&#039;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$string = &quot;MekTek&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$integer = 0;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$now = microtime(true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ($i = 0; $i &lt; 10000000; $i++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compare_value($string);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo(&quot;value: &quot; . (microtime(true) - $now) . &quot;\n&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$now = microtime(true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ($i = 0; $i &lt; 10000000; $i++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compare_ref($string);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo(&quot;ref: &quot; . (microtime(true) - $now) . &quot;\n&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$now = microtime(true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ($i = 0; $i &lt; 10000000; $i++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($str == &#039;MekTek&#039;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo(&quot;str: &quot; . (microtime(true) - $now) . &quot;\n&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$now = microtime(true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ($i = 0; $i &lt; 10000000; $i++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($integer == 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo(&quot;int: &quot; . (microtime(true) - $now) . &quot;\n&quot;);
</code></pre></p>
<p>The result:<br />
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;value: 5.2698040008545
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ref: 5.4901170730591
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str: 5.3154430389404
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int: 1.0382018089294</code></pre></p>
<p>Isn&#8217;t that fascinating.  I love it when profiling returns obvious results.  The time taken to compare a string depends on the length of the string, regardless of other factors.</p>
<p>The message is clear.  Do not compare strings in performance-critical code.  Use constants instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://philwallach.com/2009/06/17/php-profiling-comparisons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
