<?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>Straylight Run &#187; tips</title> <atom:link href="http://blog.straylightrun.net/tag/tips/feed/" rel="self" type="application/rss+xml" /><link>http://blog.straylightrun.net</link> <description>Software, Technology, PHP</description> <lastBuildDate>Mon, 07 Nov 2011 19:26:59 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>MySQL Tip Of The Day: ~/.my.cnf</title><link>http://blog.straylightrun.net/2010/09/10/mysql-tip-of-the-day-my-cnf/</link> <comments>http://blog.straylightrun.net/2010/09/10/mysql-tip-of-the-day-my-cnf/#comments</comments> <pubDate>Fri, 10 Sep 2010 21:19:34 +0000</pubDate> <dc:creator>gerard</dc:creator> <category><![CDATA[Sysadmin]]></category> <category><![CDATA[my.cnf]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[tips]]></category> <guid
isPermaLink="false">http://blog.straylightrun.net/2010/09/10/mysql-tip-of-the-day-my-cnf/</guid> <description><![CDATA[Sometimes, I want to pop onto a database server, check the status of something, and then logout. So, for example, if I want to check on the number query cache free blocks, I run this long command: % mysqladmin -u admin -p extended &#124; grep -i qcache Then I type in the password. Well, I [...]]]></description> <content:encoded><![CDATA[<p>Sometimes, I want to pop onto a database server, check the status of something, and then logout. So, for example, if I want to check on the number query cache free blocks, I run this long command:</p><div
class="wp_syntax"><div
class="code"><pre class="sh" style="font-family:monospace;">% mysqladmin -u admin -p extended | grep -i qcache</pre></div></div><p>Then I type in the password. Well, I grew tired of typing in the extra options, plus the password. Turns out, MySQL will look for the configuration file <code>.my.cnf</code> in your home directory after it looks in /etc/my.cnf (<a
href="http://dev.mysql.com/doc/refman/5.1/en/option-files.html">it looks in a few other places as well</a>). So I put this in my <code>~/.my.cnf</code>:</p><div
class="wp_syntax"><div
class="code"><pre class="sh" style="font-family:monospace;">[client]
user=admin
password=secret</pre></div></div><p>And now I can simply run:</p><div
class="wp_syntax"><div
class="code"><pre class="sh" style="font-family:monospace;">% mysqladmin extended | grep -i qcache</pre></div></div><p>and it works right away.&#160; Note that the password is stored in the clear.</p> ]]></content:encoded> <wfw:commentRss>http://blog.straylightrun.net/2010/09/10/mysql-tip-of-the-day-my-cnf/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Tip Of The Day: Avoid MySQL Functions</title><link>http://blog.straylightrun.net/2009/09/23/tip-of-the-day-avoid-mysql-functions/</link> <comments>http://blog.straylightrun.net/2009/09/23/tip-of-the-day-avoid-mysql-functions/#comments</comments> <pubDate>Wed, 23 Sep 2009 18:45:00 +0000</pubDate> <dc:creator>gerard</dc:creator> <category><![CDATA[Coding]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[query cache]]></category> <category><![CDATA[tips]]></category> <guid
isPermaLink="false">http://blog.straylightrun.net/2009/09/23/tip-of-the-day-avoid-mysql-functions/</guid> <description><![CDATA[Since I knew that the MySQL Query Cache used the literal queries as keys, it made sense that MySQL did not cache queries with certain SQL functions in them, such as this one: 1 $sql = &#34;select event_id from events where event_dt &#62;= curdate()&#34;; Because MySQL knows that this query run today is not the [...]]]></description> <content:encoded><![CDATA[<p><a
href="http://blog.straylightrun.net/2009/03/13/mysql-query-cache-or-vertical-partitioning-intro/">Since I knew</a> that the MySQL Query Cache used the literal queries as keys, it made sense that MySQL did not cache queries with certain SQL functions in them, such as this one:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select event_id from events where event_dt &gt;= curdate()&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>Because MySQL knows that this query run today is not the same query when it is run tomorrow. There are other SQL functions such as <code>rand()</code> and <code>unix_timestamp()</code> that will bypass the query cache. These are <a
href="http://dev.mysql.com/doc/refman/5.0/en/query-cache-how.html">listed here</a>.</p><p>So I avoid these functions when possible by calculating the value in PHP. For example, I’d rewrite the above query as:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select event_id from events where event_dt &gt;= '<span style="color: #006699; font-weight: bold;">$date</span>'&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div> ]]></content:encoded> <wfw:commentRss>http://blog.straylightrun.net/2009/09/23/tip-of-the-day-avoid-mysql-functions/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Tip Of The Day: Removing .htaccess</title><link>http://blog.straylightrun.net/2009/06/17/tip-of-the-day-removing-htaccess/</link> <comments>http://blog.straylightrun.net/2009/06/17/tip-of-the-day-removing-htaccess/#comments</comments> <pubDate>Wed, 17 Jun 2009 20:48:21 +0000</pubDate> <dc:creator>gerard</dc:creator> <category><![CDATA[Performance]]></category> <category><![CDATA[Sysadmin]]></category> <category><![CDATA[apache]]></category> <category><![CDATA[htaccess]]></category> <category><![CDATA[tips]]></category> <guid
isPermaLink="false">http://blog.straylightrun.net/?p=205</guid> <description><![CDATA[At work, every project has an .htaccess file containing at the least some mod_rewrite rules.  This way, all I need to do to run a project is check it out of version control.  I don&#8217;t need to modify my local Apache configuration. But turning this option on and allowing .htaccess files may be a performance [...]]]></description> <content:encoded><![CDATA[<p>At work, every project has an <code>.htaccess</code> file containing at the least some <code>mod_rewrite </code>rules.  This way, all I need to do to run a project is check it out of version control.  I don&#8217;t need to modify my local Apache configuration.</p><p>But turning this option on and allowing <code>.htaccess</code> files may be a performance hit.  More specifically, enabling the <code><a
href="http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride">AllowOverride</a> </code>option in Apache is a performance hit.  The <a
href="http://httpd.apache.org/docs/2.2/misc/perf-tuning.html">Apache docs</a> sums up the problem best:</p><blockquote><p> &#8220;Wherever in your URL-space you allow overrides (typically <code>.htaccess</code> files) Apache will attempt to open <code>.htaccess</code> for each filename component. For example,</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
</pre></td><td
class="code"><pre class="xml" style="font-family:monospace;">DocumentRoot /www/htdocs
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Directory</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   AllowOverride all
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div><p>and a request is made for the URI <code>/index.html</code>. Then Apache will attempt to open <code>/.htaccess</code>, <code>/www/.htaccess</code>, and <code>/www/htdocs/.htaccess</code>.&#8221;</p></blockquote><p>So I disabled all <code>.htaccess </code>files in production, and inserted each file&#8217;s individual <code>mod_rewrite </code>rules into the main Apache config file. After a quick <a
href="http://blog.straylightrun.net/2009/04/23/apache-bench/">Apache Bench</a> run, one project looked around 3% faster. Note that there are a few other useful optimizations on that page.</p> ]]></content:encoded> <wfw:commentRss>http://blog.straylightrun.net/2009/06/17/tip-of-the-day-removing-htaccess/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Tip Of The Day: ON DUPLICATE KEY UPDATE</title><link>http://blog.straylightrun.net/2009/02/09/tip-of-the-day-on-duplicate-key-update/</link> <comments>http://blog.straylightrun.net/2009/02/09/tip-of-the-day-on-duplicate-key-update/#comments</comments> <pubDate>Mon, 09 Feb 2009 21:38:19 +0000</pubDate> <dc:creator>gerard</dc:creator> <category><![CDATA[Coding]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[tips]]></category> <guid
isPermaLink="false">http://blog.straylightrun.net/?p=111</guid> <description><![CDATA[In MySQL (and MySQL only AFAIK), INSERT has a clause called ON DUPLICATE KEY UDPATE. When ON DUPLICATE KEY UPDATE is used with INSERT, the insert will update the record if a value for a unique or primary key already exists, or else create a record if the value does not exist. So now when [...]]]></description> <content:encoded><![CDATA[<p>In MySQL (and MySQL only AFAIK), INSERT has a clause called ON DUPLICATE KEY UDPATE. When <a
href="http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html">ON DUPLICATE KEY UPDATE</a> is used with INSERT, the insert will update the record if a value for a unique or primary key already exists, or else create a record if the value does not exist. So now when a form can either create a new something or edit an existing something, you can use one query to do it, and not have to query to see if the something exists already.</p><p>So instead of this:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getone</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select count(*) from events where event_id = 15&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;update events set name = 'New name' where event_id = 15&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;insert into events (event_id, name) values (null, 'New name')&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>you can do something like this:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;insert into events (event_id, name) values (<span style="color: #006699; font-weight: bold;">$id</span>, 'New name') on duplicate key update name = 'New name'&quot;</span><span style="color: #339933;">;</span></pre></div></div><p>Neat, eh?</p><p>In case you&#8217;re wondering what the difference is between ON DUPLICATE KEY UPDATE and a REPLACE query, a REPLACE fires a DELETE followed by an INSERT query, as opposed to a real UPDATE.</p> ]]></content:encoded> <wfw:commentRss>http://blog.straylightrun.net/2009/02/09/tip-of-the-day-on-duplicate-key-update/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Tip Of The Day: Debug Logging</title><link>http://blog.straylightrun.net/2008/12/12/tip-of-the-day-debug-logging/</link> <comments>http://blog.straylightrun.net/2008/12/12/tip-of-the-day-debug-logging/#comments</comments> <pubDate>Fri, 12 Dec 2008 14:45:15 +0000</pubDate> <dc:creator>gerard</dc:creator> <category><![CDATA[Coding]]></category> <category><![CDATA[tips]]></category> <guid
isPermaLink="false">http://blog.straylightrun.net/2008/12/12/tip-of-the-day-debug-logging/</guid> <description><![CDATA[If you find yourself wanting to echo or print something to the screen, go ahead and do it, but be sure to add a debug-level logging call for the info too. Chances are, you or someone else will want to see the same info sometime in the future.]]></description> <content:encoded><![CDATA[<p>If you find yourself wanting to echo or print something to the screen, go ahead and do it, but be sure to add a debug-level logging call for the info too. Chances are, you or someone else will want to see the same info sometime in the future.</p> ]]></content:encoded> <wfw:commentRss>http://blog.straylightrun.net/2008/12/12/tip-of-the-day-debug-logging/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Tip Of The Day: isset() vs. in_array()</title><link>http://blog.straylightrun.net/2008/12/03/tip-of-the-day-codeissetcode-vs-codein_arraycode/</link> <comments>http://blog.straylightrun.net/2008/12/03/tip-of-the-day-codeissetcode-vs-codein_arraycode/#comments</comments> <pubDate>Wed, 03 Dec 2008 16:50:26 +0000</pubDate> <dc:creator>gerard</dc:creator> <category><![CDATA[Coding]]></category> <category><![CDATA[Performance]]></category> <category><![CDATA[in_array]]></category> <category><![CDATA[isset]]></category> <category><![CDATA[tips]]></category> <guid
isPermaLink="false">http://blog.straylightrun.net/?p=43</guid> <description><![CDATA[When looking for something in an array of values, it is very tempting to use in_array(). After all, that’s what the name says. However, searching through an array, even with best-case search algorithms, will never be faster than a single index lookup, which is where isset() comes in. With isset(), you can use one operation [...]]]></description> <content:encoded><![CDATA[<p>When looking for something in an array of values, it is very tempting to use <a
href="http://php.net/in_array"><code>in_array()</code></a>. After all, that’s what the name says. However, searching through an array, even with best-case search algorithms, will never be faster than a single index lookup, which is where <a
href="http://php.net/isset"><code>isset() </code></a>comes in. With <code>isset()</code>, you can use one operation to see if a value exists, provided those values exist as keys. I don’t know if it’s truly random access, but it’s pretty darn close.</p><p>So, instead of something like this:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$exclude</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$size</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exclude</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// do something</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>do something like:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$exclude</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$exclude</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$exclude</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$exclude</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</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: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$size</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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$exclude</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// do something</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>So does this make a difference?  Let&#8217;s write a little benchmark script.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">#!/usr/bin/php
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$haystack</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</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;">1000</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: #000088;">$haystack</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$needles</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</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;">1000</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: #000088;">$needles</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1000</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;">1000</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: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$needles</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>We fill two arrays with 1000 random integers. One is the haystack &#8211; what we will search through. The other is the list of needles &#8211; we want to search for each one. For each needle, we look for it in the haystack. Then, we repeat this 1000 times.</p><p>Executing this, the script takes around 37 seconds:</p><div
class="wp_syntax"><div
class="code"><pre class="sh" style="font-family:monospace;">% time ./bench.php
&nbsp;
real    0m37.400s
user    0m37.282s
sys     0m0.068s</pre></div></div><p>Now, let&#8217;s change the last <code>for()</code> loop to this:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>15
16
17
18
19
20
21
22
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><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;">1000</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: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_flip</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$needles</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tmp</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>The new output:</p><div
class="wp_syntax"><div
class="code"><pre class="sh" style="font-family:monospace;">% time ./bench.php
&nbsp;
real    0m0.778s
user    0m0.764s
sys     0m0.008s</pre></div></div><p>Execution time drops from around 37 seconds to 0.7 seconds.</p> ]]></content:encoded> <wfw:commentRss>http://blog.straylightrun.net/2008/12/03/tip-of-the-day-codeissetcode-vs-codein_arraycode/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
