<?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>Mike Gerwitz &#187; Development</title>
	<atom:link href="http://mikegerwitz.com/cat/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://mikegerwitz.com</link>
	<description>Free Software Developer</description>
	<lastBuildDate>Mon, 26 Jul 2010 20:02:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Performance: Array Iteration</title>
		<link>http://mikegerwitz.com/2010/03/28/php-performance-array-iteration/</link>
		<comments>http://mikegerwitz.com/2010/03/28/php-performance-array-iteration/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 12:32:11 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Performance]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/?p=273</guid>
		<description><![CDATA[In PHP, arrays are used for virtually everything (I&#8217;ll spare my rant on that), therefore it&#8217;s likely that you&#8217;re going to use them abundantly in your scripts. The issue we&#8217;re going to be focusing on here isn&#8217;t with the arrays themselves &#8211; it&#8217;s the simple fact that you&#8217;re probably going to have to iterate over [...]]]></description>
			<content:encoded><![CDATA[<p>In <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>, arrays are used for virtually everything (I&#8217;ll spare my rant on that), therefore it&#8217;s likely that you&#8217;re going to use them abundantly in your scripts. The issue we&#8217;re going to be focusing on here isn&#8217;t with the arrays themselves &#8211; it&#8217;s the simple fact that you&#8217;re probably going to have to iterate over them in order to get the data you want. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> offers a wide range of methods for doing so, so which do we use?</p>
<p>First, let&#8217;s generate an array to work with. We&#8217;re going for quantity here, not size of data. Therefore, we&#8217;ll simply generate an array containing a list of integers. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> offers a quick and easy way to do this using the <a href="http://us.php.net/manual/en/function.array_fill.php"><tt>array_fill()</tt></a> function:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code14'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27314"><td class="code" id="p273code14"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// generate an array containing 100,000 elements, each with the value of 1</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 0<span style="color: #339933;">,</span> 100000<span style="color: #339933;">,</span> 1 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We want to use the <tt>array_fill()</tt> function rather than <tt>range()</tt> here because we will be summing up the values in the examples below. Range will generate incremental values, leading to integer overflows and inconsistent results across various architectures.</p>
<p><strong>Note:</strong> This article assumes that the reader is running at least <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5.0.0. If you are running anything else, you should understand that <a href=http://www.php.net/archive/2008.php#id2008-08-07-1">official support for <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 4 ended back in 2008</a> and you should consider upgrading immediately.</p>
<h3><tt>for</tt> loop</h3>
<p>Those coming from a procedural background may be most familiar with the <a href="http://us2.php.net/manual/en/control-structures.for.php"><tt>for</tt> loop</a> as a means of iterating through an array. Since it is so common, let&#8217;s take a look at that first:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code15'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27315"><td class="code" id="p273code15"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$time_start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><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;">$total</span>      <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</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> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><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;">++</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</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: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate total running time and output the result</span>
<span style="color: #000088;">$time_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><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;">$time_start</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Total: <span style="color: #009933; font-weight: bold;">%d</span><span style="color: #000099; font-weight: bold;">\n</span>loop time: <span style="color: #009933; font-weight: bold;">%f</span>ms<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time_end</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Let&#8217;s run that test and see what type of results we get:</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.127712ms
</p></blockquote>
<p>Ouch. The time that is returned from the above test will depend on the speed of your system. I do know that, on my system, that&#8217;s a lot of time to spend simply adding up elements of an array. Let&#8217;s see how we may be able to improve that a bit.</p>
<p>The first thing that may jump out is the use of the <tt>count</tt> method in the condition of the <tt>for</tt> clause:</p>
<blockquote><p><tt>for ( $i = 0; $i &lt; <b>count( $data )</b>; $i++ )</tt></p></blockquote>
<p>It may help to understand exactly how <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> executes the above statement. The assignment portion of the loop is executed once before any iterations begin. The step/count, which is the <tt>$i++</tt> part, is executed after each iteration. The test expression (middle) is executed before each iteration to determine if the step should be executed and if we should continue with the loop.</p>
<p>Alright, that&#8217;s pretty straightforward. But that is the issue. That <tt>count</tt> function is being executed <em>for every iteration</em>. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is not caching the result of that function. That gets pretty expensive considering that it would be calling the function no less than 100,000 times.</p>
<p>Let&#8217;s see what happens when we change it up a bit:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code16'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27316"><td class="code" id="p273code16"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> 0<span style="color: #339933;">,</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><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;">$count</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;">$total</span> <span style="color: #339933;">+=</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: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ...</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
for loop time: 0.046609ms
</p></blockquote>
<p>Wow &#8211; that is a huge improvement! We nearly tripled the speed of the iteration simply by having the conditional check a precalculated value, rather than calling the function with each iteration. That&#8217;s pretty good, right?</p>
<p>No; it&#8217;s still pretty slow. We can do much better.</p>
<h3><tt>while</tt> loop and <tt>current()</tt></h3>
<p>Some people may prefer to use a <a href="http://us2.php.net/manual/en/control-structures.while.php"><tt>while</tt> loop</a> in order to loop through the elements of an array. One of the most common ways of doing that is to use the <a href="http://us2.php.net/manual/en/function.current.php"><tt>current()</tt></a> function in conjunction with <a href="http://us2.php.net/manual/en/function.next.php"><tt>next()</tt></a>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code17'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27317"><td class="code" id="p273code17"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/current"><span style="color: #990000;">current</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/next"><span style="color: #990000;">next</span></a><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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So how does that fair speed wise. It certainly <em>looks</em> a little less complicated than the <tt>for</tt> loop&#8230;that means it&#8217;s faster, right?</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.175094ms
</p></blockquote>
<p>Yikes. We took a step in the wrong direction. Let&#8217;s pretend we didn&#8217;t see this.</p>
<p><a id="foreach"></a></p>
<h3><tt>foreach</tt> loop</h3>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 4 introduced the <a href="http://us2.php.net/manual/en/control-structures.foreach.php"><tt>foreach</tt> construct</a> which provides a much more simple means of iterating through an array. Surely if it was designed for arrays, it should be faster than <tt>for</tt>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code18'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27318"><td class="code" id="p273code18"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Notice that, in the above snippet, we are also avoiding the use of a temporary variable <tt>$i</tt> to store the current index. While <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is undoubtedly doing so internally, we&#8217;re talking about a difference between compiled and optimized C code versus interpreted, sluggish <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> code. So that alone will provide us with a bit of a benefit, however slight.</p>
<p>Let&#8217;s see the result:</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.037076ms
</p></blockquote>
<p>Not too bad&#8230;we managed to shave off nearly 1/4th the time of the <tt>for</tt> loop. But I&#8217;m not satisfied.</p>
<h3>The Problem</h3>
<p>What do all of the above have in common? They&#8217;re looping structures. Great, so what&#8217;s the issue? If you&#8217;re coming from a compiled, high-performance language like C, there is no issue. Let&#8217;s see how long it&#8217;d take to iterate through an array containing 100,000 elements in C:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code19'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27319"><td class="code" id="p273code19"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/time.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define DATALEN 100000</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span>            data<span style="color: #009900;">&#91;</span> DATALEN <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">register</span> <span style="color: #993333;">long</span>  i<span style="color: #339933;">;</span>  
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span>  total <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">struct</span> timeval time_start<span style="color: #339933;">,</span> time_end<span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span>           elapsed<span style="color: #339933;">,</span> uelapsed<span style="color: #339933;">;</span> 
    <span style="color: #993333;">double</span>         elapsed_total<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// create our data array</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> DATALEN<span style="color: #339933;">;</span> data<span style="color: #009900;">&#91;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> 1 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// get current time</span>
    gettimeofday<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>time_start<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #666666; font-style: italic;">// loop through the array summing up the values</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> DATALEN<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>   
        total <span style="color: #339933;">+=</span> data<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>   
&nbsp;
    <span style="color: #666666; font-style: italic;">// get total running time</span>
    gettimeofday<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>time_end<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    elapsed       <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> time_end.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> time_start.<span style="color: #202020;">tv_sec</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    uelapsed      <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> time_end.<span style="color: #202020;">tv_usec</span> <span style="color: #339933;">-</span> time_start.<span style="color: #202020;">tv_usec</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    elapsed_total <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> elapsed <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> uelapsed <span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> 1000000 <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// output total and loop time</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;Total: %ld<span style="color: #000099; font-weight: bold;">\n</span>loop time: %fms<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
        total<span style="color: #339933;">,</span> elapsed_total
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
loop time: 0.000273ms
</p></blockquote>
<p>Well, then. The point here is &#8211; <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is a slow language, <em>especially</em> for looping. Wherever possible, you want to avoid it.</p>
<p>But you need to iterate through your arrays somehow! Does that mean you are stuck with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s slow loops?</p>
<p><a id="builtin"></a></p>
<h3>Use Built-In Functions</h3>
<p>Fortunately, there&#8217;s certain functions available to you that can drastically improve the speed of array processing. In fact, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> has a whole slew of <a href="http://us2.php.net/manual/en/ref.array.php">functions devoted to processing and altering arrays</a>.</p>
<p>Programmers coming form other languages such as C may prefer to write their own loops for processing data, because it is faster than a function call. This is not true at all with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>; in fact, much of <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s power comes from it&#8217;s built-in functions. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> itself and its libraries are written in C. Therefore, if you can use a built-in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> function that will do the looping for you, that loop will be executed much more quickly.</p>
<p>Let&#8217;s take another look at our above example &#8211; summing up 100,000 values of an array. Is there a <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> function that could do this for us? <a href="http://us2.php.net/manual/en/function.array-sum.php"><tt>array_sum()</tt></a> perhaps?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code20'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27320"><td class="code" id="p273code20"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$time_start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><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>
&nbsp;
<span style="color: #666666; font-style: italic;">// sum up the values in the array</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_sum"><span style="color: #990000;">array_sum</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate total running time and output the result</span>
<span style="color: #000088;">$time_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><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;">$time_start</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Total: <span style="color: #009933; font-weight: bold;">%d</span><span style="color: #000099; font-weight: bold;">\n</span>for loop time: <span style="color: #009933; font-weight: bold;">%f</span>ms<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time_end</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Above, we are making a single function call to <tt>array_sum()</tt> rather than going in a loop and manually adding everything up. Let&#8217;s see how this method does:</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.012571ms
</p></blockquote>
<p>Not bad. Using everything discussed here, we&#8217;ve cut our time down from the original 0.127712ms to a mere 0.012571ms. It&#8217;s still no C code, but our loop is <em>10 times faster</em> than it was originally. It&#8217;s still written in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>; we&#8217;re just using the tools we were given.</p>
<p>But wait &#8211; I said that <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s libraries were written in C. Why isn&#8217;t <tt>array_sum()</tt> just as fast as the C example provided above? Unfortunately, we&#8217;re still dealing with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s variables and other internal representations here. It&#8217;s not just jumping to a position in memory like it does with an array in C. The <tt>array_sum()</tt> function still has to scan the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> array to retrieve all the values. There&#8217;s no getting away from that.</p>
<h3>Expense of Function Calls</h3>
<p>One must also be mindful of <em>what</em> their loop is doing. Take a look at the following example. It calls a function in order to count all the values in an array that contain the number 2.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code21'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27321"><td class="code" id="p273code21"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> check_value<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #339933;">===</span> 2 <span style="color: #009900;">&#41;</span>
        ? 1
        <span style="color: #339933;">:</span> 0
    <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// generate an array with 1/3 values containing 1, the rest containing 2</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>
    <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 0<span style="color: #339933;">,</span> 10000<span style="color: #339933;">,</span> 1 <span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">+</span> <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 10000<span style="color: #339933;">,</span> 20000<span style="color: #339933;">,</span> 2 <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$time_start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><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>
&nbsp;
<span style="color: #666666; font-style: italic;">// count the values in the array greater than 50000</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$count</span> <span style="color: #339933;">+=</span> check_value<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate total running time and output the result</span>
<span style="color: #000088;">$time_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><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;">$time_start</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Count: <span style="color: #009933; font-weight: bold;">%d</span><span style="color: #000099; font-weight: bold;">\n</span>for loop time: <span style="color: #009933; font-weight: bold;">%f</span>ms<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$count</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time_end</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
Count: 20000<br />
for loop time: 0.043597ms
</p></blockquote>
<p>Above, we called the <tt>check_value()</tt> function 30,000 times. Functions do an excellent job at improving code clarity and reducing duplicate code. However, function and method calls are expensive. Consider what happens when we get rid of the function and do all the processing in the body of the loop:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code22'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27322"><td class="code" id="p273code22"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// count the values in the array greater than 50000</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</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: #000088;">$val</span> <span style="color: #339933;">===</span> 2 <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If we run it again using that loop instead of the previous one, we get the following result:</p>
<blockquote><p>
Count: 20000<br />
for loop time: 0.013397ms
</p></blockquote>
<p>That&#8217;s a significant speed improvement. Now, I&#8217;m not advocating that you get rid of all of your functions (though I would advocate preprocessor macros in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>). However, consider the performance implications of excessive function or method calls where it is important. In most situations, the benefits you gain from legibility will outweigh the performance pitfalls.</p>
<p>It should also be noted that under many circumstances, the overhead of the function call may be insignificant. If your method takes 0.3 seconds to execute, you&#8217;re not going to care about milliseconds of overhead.</p>
<h3>Memory Tradeoff &#038; Apparent Processing</h3>
<p>Let&#8217;s continue with the example from the previous section (counting the number of elements in an array that contain the value <tt>2</tt>). <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> does not contain a function that can explicitly search an array for the given value and return the number of results. It does, however, have something very similar &#8211; <a href="http://us.php.net/manual/en/function.array-count-values.php"><tt>array_count_values()</tt></a>.</p>
<p>The <tt>array_count_values()</tt> function will loop through the array and maintain a count of each of the values. It will then return an array containing the results. Therefore, in our above example, it would return an array much like the following:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code23'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27323"><td class="code" id="p273code23"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10000</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// there are 10,000 1s in our data array</span>
    <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">20000</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// there are 20,000 2s in our data array</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s a bit more information than we want. In fact, what if we had an array containing hundreds of different values? Wouldn&#8217;t it take longer to count every element than a single one (well, no, but one might assume)? That would account for a bit more memory usage as well, even though it does give us the answer we are looking for. Which is a more elegant solution?</p>
<p>When attempting to optimize code, developers must often have to choose between CPU time and memory footprint. This is one of those situations due to language limitations. Let&#8217;s take a look at how much time it takes for <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> to count the values for us:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code24'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27324"><td class="code" id="p273code24"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// count the values in the array greater than 50000</span>
<span style="color: #000088;">$count_data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_count_values"><span style="color: #990000;">array_count_values</span></a><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;">$count</span>      <span style="color: #339933;">=</span> <span style="color: #000088;">$count_data</span><span style="color: #009900;">&#91;</span> 2 <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// output memory consumption</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Memory Consumption: <span style="color: #009933; font-weight: bold;">%d</span> bytes<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/memory_get_usage"><span style="color: #990000;">memory_get_usage</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
Memory Consumption: 6815744 bytes<br />
Count: 20000<br />
for loop time: 0.003805ms
</p></blockquote>
<p>Even though we&#8217;re gathering more information, it&#8217;s still 3.5x faster than counting only a single value manually. In fact, I&#8217;m willing to bet that it even consumed less memory than our previous example, simply due to the fact that we aren&#8217;t having to deal with userland variables and function calls:</p>
<blockquote><p>
(Our example using check_value(), with memory output)</p>
<p>Memory Consumption: 7077888 bytes<br />
Count: 20000<br />
for loop time: 0.043128ms
</p></blockquote>
<p>It seems that counting a single element using our <tt>check_value()</tt> method used <tt>262144</tt> more bytes than <tt>array_count_values()</tt>! Damn you, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>! Yes, it&#8217;s unfortunate that considerations like this must be made, but you&#8217;d be surprised how many internal methods really do provide both strong memory and CPU benefits even though they &#8220;do more work&#8221;. Userland consumes considerable amounts of memory.</p>
<p>That isn&#8217;t to say that <tt>array_count_values()</tt> can&#8217;t consume more memory. Like I said previously &#8211; if it returns an array with thousands of values, it&#8217;s going to offset that memory savings.</p>
<h3>ArrayObjects and Iterators</h3>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5 began a strong move toward Object-Oriented programming. It provides a number of powerful components that deal with iteration &#8211; specifically <a href="http://us.php.net/manual/en/class.traversable.php"><tt>Traversable</tt></a> objects and <a href="http://us.php.net/manual/en/class.iterator.php"><tt>Iterators</tt></a>. They provide a benefit to developers in the sense that it makes code much easier to understand and allows for powerful OO implementations, but does it provide any performance benefit?</p>
<p>Firstly, let&#8217;s see what happens when we loop through an <a href="http://us.php.net/manual/en/class.arrayobject.php"><tt>ArrayObject</tt></a> rather than a normal <tt>array</tt>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code25'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27325"><td class="code" id="p273code25"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// generate an array containing 100,000 elements, each with the value of 1</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 0<span style="color: #339933;">,</span> 100000<span style="color: #339933;">,</span> 1 <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
loop time: 0.061663ms
</p></blockquote>
<p>This is compared to our <tt>foreach</tt> example with a result of <tt>0.037076ms</tt>. So we did loose a bit of time there. However, that does not mean you shouldn&#8217;t use <tt>ArrayObject</tt>s. In fact, I use them a great deal in OO scripts, because <tt>ArrayObject</tt>s coupled with <tt>Iterator</tt>s provide powerful designs that aren&#8217;t quite as elegant when using procedural code. However, when performance is a requirement, I have to steer clear of them.</p>
<p>You will find that the <tt>Iterator</tt> object provides very little benefit over a conventional <tt>foreach</tt> loop as well. The only time I make use of an <tt>Iterator</tt> is when it contains additional logic that determines what results it should return (e.g. to iterate only over odd numbers), or if the implementation allows for an <tt>Iterator</tt> to be passed in at runtime to determine what results should be returned.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p273code26'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p27326"><td class="code" id="p273code26"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #000088;">$iterator</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
loop time: 0.291542ms
</p></blockquote>
<p>In fact, that&#8217;s our slowest method yet. So, just be wise when weighing the costs and benefits. If performance doesn&#8217;t matter &#8211; go ahead. Use whatever implementation is best for you. That&#8217;s why you have the option. If performance <em>is</em> a factor, don&#8217;t even glance at those objects. Maybe future versions of <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> will enhance the functionality to the point where their speeds are equivalent to the loop structures.</p>
<h3>Wall Of Text Summary</h3>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> provides a number of ways to iterate through arrays &#8211; rest assured that we haven&#8217;t nearly covered them all here. Out of all the loop structures available, <a href="#foreach"><tt>foreach</tt></a> is the fastest for iteration. However, it comes up short when compared to <a href="#builtin">built-in functions</a> designed for array processing. Where possible, those built-in functions should be used, even where it may seem at first glance that it may be slower. They often provide a very strong performance benefit &#8211; both CPU and memory wise.</p>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5 introduced a number of new means of iteration through the use of objects. Certain implementations may do well to improve code legibility, and may enable the developer to implement some fairly powerful object-oriented designs, but when performance is a factor, you want to steer clear of them.</p>
<p>Hopefully this article helped to clear up some questions. I expect not to see any more <tt>for</tt> loops with <tt>count()</tt> in the loop test expression!</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2010%2F03%2F28%2Fphp-performance-array-iteration%2F&amp;linkname=PHP%20Performance%3A%20Array%20Iteration">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2010/03/28/php-performance-array-iteration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Extension Writing: Namespaces</title>
		<link>http://mikegerwitz.com/2009/06/21/php-extension-writing-namespaces/</link>
		<comments>http://mikegerwitz.com/2009/06/21/php-extension-writing-namespaces/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 08:53:47 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/?p=97</guid>
		<description><![CDATA[I&#8217;m fairly new to PHP extension writing. I&#8217;ve just gotten into it in order to optimize some existing PHP 5.3 code for my WebKernel project. However, there was one fundamental problem that I could not find the solution to anywhere online &#8211; how do you define a function within a namespace!? WebKernel used namespaces extensively, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m fairly new to <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> extension writing. I&#8217;ve just gotten into it in order to optimize some existing <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5.3 code for my WebKernel project. However, there was one fundamental problem that I could not find the solution to anywhere online &#8211; <em>how do you define a function within a namespace!? </em>WebKernel used namespaces extensively, so I needed to know the solution to the problem.</p>
<p>Namespaces are new as of <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5.3. Therefore, there&#8217;s very few people actually writing extensions with namespace support. To make matters worse, documentation of the Zend core is very poor. Much of my time is spent reading through the code, trying to figure out what macros do what, etc. This was another one of those cases.</p>
<h1>Normal <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> Functions:</h1>
<p>Normal <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> functions are placed within the functions list using the <tt>PHP_FE</tt> macro. The actual function is defined using <tt>PHP_FUNCTION</tt>. So your code may look like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p97code34'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9734"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p97code34"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> function_entry my_functions<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
PHP_FE<span style="color: #009900;">&#40;</span> myfunc<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
PHP_FUNCTION<span style="color: #009900;">&#40;</span> myfunc <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// Function body here</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you were to call <tt>myfunc()</tt> from within userland (within a <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> script), it would execute the C code defined within the body of <tt>PHP_FUNCTION( myfunc )</tt>. Simple enough. Now what if we wanted namespaces?</p>
<h1>Using Namespaces</h1>
<p><strong>The short answer:</strong><br />
Replace <tt>PHP_FE( myfunc, NULL )</tt> with <tt>ZEND_NS_FE( "myns", myfunc, NULL )</tt>. But if you actually want to know why, keep reading.</p>
<p><strong>The detailed answer:</strong><br />
<tt>PHP_FE</tt> is defined within <tt>main/php.h</tt>. That points us to the <tt>ZEND_FE</tt> macro defined within <tt>Zend/zend_API.h</tt>. While looking through this file, I noticed a <tt>ZEND_NS_FE</tt> macro in there, and figured it must be referring to namespaces. So I figured that logically, there would be a <tt>PHP_NS_FE</tt> macro, right? Nope. There&#8217;s no such macro defined anywhere. At least not at this point in time. Hopefully there will be for the final release of 5.3.<br />
 yourself, however that will ruin your chances of your extension becoming portable. Other developers aren&#8217;t going to want to patch their php.h file to use it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p97code35'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9735"><td class="code" id="p97code35"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define ZEND_FE(name, arg_info)						ZEND_FENTRY(name, ZEND_FN(name), arg_info, 0)</span>
<span style="color: #339933;">#define ZEND_NS_FE(ns, name, arg_info)					ZEND_NS_FENTRY(ns, name, ZEND_FN(name), arg_info, 0)</span></pre></td></tr></table></div>

<p>Looking over the macros, you see that both macros make use of the <tt>ZEND_FN</tt> macro. A quick glance at that macro shows that it converts our &#8220;myfunc&#8221; function into &#8220;zif_myfunc&#8221;:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p97code36'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9736"><td class="code" id="p97code36"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define ZEND_FN(name) zif_##name</span></pre></td></tr></table></div>

<p>This means that internally, zend will use the same function name regardless of whether or not it is contained within a namespace. In other words, we don&#8217;t even have to touch our <tt>PHP_FUNCTION</tt> line! Great &#8211; so then what&#8217;s different? Let&#8217;s take a look at <tt>ZEND_NS_FENTRY</tt>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p97code37'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9737"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p97code37"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define ZEND_NS_FENTRY(ns, zend_name, name, arg_info, flags)		ZEND_RAW_FENTRY(ZEND_NS_NAME(ns, #zend_name), name, arg_info, flags)</span></pre></td></tr></table></div>

<p>The difference here is that Zend is making use of the <tt>ZEND_NS_NAME</tt> macro:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p97code38'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9738"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p97code38"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define ZEND_NS_NAME(ns, name)			ns&quot;\\&quot;name</span></pre></td></tr></table></div>

<p>Ah-ha! (Ignoring the fact that it&#8217;s, at present, at the top of the file.) While internally our function name is no different, Zend will only call it if it is prefixed with the namespace name from within userland. The above macro converts our function name to &#8220;myns\myfunc&#8221;. So, our updated code may look like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p97code39'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9739"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p97code39"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define MY_NS &quot;myns&quot;&lt;/code&gt;</span>
&nbsp;
<span style="color: #993333;">static</span> function_entry my_functions<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
ZEND_NS_FE<span style="color: #009900;">&#40;</span> MY_NS<span style="color: #339933;">,</span> myfunc<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
PHP_FUNCTION<span style="color: #009900;">&#40;</span> myfunc <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// Function body here</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We can then call our function via &#8220;myns\myfunc&#8221;. Remember &#8211; you must use <tt>ZEND_NS_FE</tt>. As of this time of writing, <tt>PHP_NS_FE</tt> does not exist! Of course, that could easily be solved by adding it to your own source files yourself, but it&#8217;s still a messy task. I&#8217;ve submitted a bug report to the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> team detailing the additions (I couldn&#8217;t find where to submit a patch), so hopefully it&#8217;ll make it in. Until then, feel free to add this to your header file, along with any other <tt>ZEND_NS_*</tt> aliases you may need:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p97code40'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9740"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p97code40"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifndef PHP_NS_FE</span>
<span style="color: #339933;">#    define PHP_NS_FE ZEND_NS_FE</span>
<span style="color: #339933;">#endif</span></pre></td></tr></table></div>

<h1>Summary</h1>
<p>While diving into the C code isn&#8217;t necessary to use namespaces, it does greatly improve your understanding of how the system works. Because there is so little documentation, I encourage you to keep looking through <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s source and see what you find. There are goodies hidden everywhere that may be incredibly useful to you, and save you a lot of time. Look through existing extensions within the <tt>ext</tt> folder and see if any of them use methods that you are interested in.</p>
<p>It was the unfortunate circumstance that none of the extensions did make use of namespace support. So I was on my own to look for it. I hope this helps save some frustration for others, especially those new to Zend that are just trying to figure out whether or not porting their code to C is worth the effort.</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2009%2F06%2F21%2Fphp-extension-writing-namespaces%2F&amp;linkname=PHP%20Extension%20Writing%3A%20Namespaces">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2009/06/21/php-extension-writing-namespaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP6 Snapshot &#8211; Namespace Separator Patch Applied</title>
		<link>http://mikegerwitz.com/2008/12/18/php6-snapshot-namespace-separator-patch-applied/</link>
		<comments>http://mikegerwitz.com/2008/12/18/php6-snapshot-namespace-separator-patch-applied/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 10:59:16 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/2008/12/18/php6-snapshot-namespace-separator-patch-applied/</guid>
		<description><![CDATA[In my MyCustomBB Blog a while back, I discussed the PHP&#8217;s decision to change the namespace separator to a backslash. Until recently, this patch hadn&#8217;t yet been applied. Therefore, I continued developing my code using the old static operator (::).
Well, I recently installed the new PHP 6 snapshot from http://snaps.php.net/ and found that my code [...]]]></description>
			<content:encoded><![CDATA[<p>In my MyCustomBB Blog a while back, I discussed the <a href="http://dev.mycustombb.com/blog/2008/10/27/namespaces-core-design-separator-change/"><acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s decision to change the namespace separator to a backslash</a>. Until recently, this patch hadn&#8217;t yet been applied. Therefore, I continued developing my code using the old static operator (::).</p>
<p>Well, I recently installed the new <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 6 snapshot from <a href="http://snaps.php.net/">http://snaps.php.net/ </a>and found that my code was broken &#8211; a pleasent surprise I&#8217;m sure to many <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> developers. Just think about the ones who don&#8217;t even know about the change. Imagine that frustration. Anyway, I quickly realized that the separator had changed. Thankfully, it&#8217;s pretty much a simple search-and-replace to modify my namespaces to use the new separator using the sed utility. Be sure to back up your data before performing any batch replacing on your scripts.</p>
<p>I suppose I could help out by providing a script to help you make that change. In my situation, all I have to do is replace &#8220;webkernel::&#8221; with &#8220;webkernel\&#8221;, and &#8220;cli::&#8221; with, &#8220;cli\&#8221;. Simply paste the below command into a <acronym title="Bourne Again Shell">BASH</acronym> terminal within the root directory of your project, and it will recursively replace all occurances in all <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> files. Again, <em>BACK UP YOUR WORK!</em> I&#8217;m not to be held responsible for any irreversible damage done to your code. Be sure you understand the command before running it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p43code42'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4342"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p43code42"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-lt</span> 2 <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> Usage: rsed <span style="color: #ff0000;">&quot;FILEPATTERN&quot;</span> <span style="color: #ff0000;">&quot;REGEXP&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> 1
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;$1&quot;</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #666666; font-style: italic;"># Make a backup</span>
    <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$i</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>.bak&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Perform the replacement</span>
    <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">&quot;$2&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>.bak&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #007800;">$i</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Remove the backup - comment out to keep backup</span>
     <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>.bak&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> Done.</pre></td></tr></table></div>

<p>Save the above as <tt>rsed</tt>, chmod +x, and run it using the following as an example: <tt>./rsed "*.php" "s/webkernel::/webkernel\\\\g/"</tt> (in the previous command, &#8220;\\\\&#8221; = &#8220;\&#8221;). Ensure you use an extension like &#8220;*.php&#8221; for the file pattern &#8211; don&#8217;t just use &#8220;*&#8221; or it&#8217;ll cause problems, as it&#8217;ll match directories as well. If you have files that contain no extension, replace them manually.</p>
<p>Alternatively, in the above script, you may instead decide to use <tt>grep -rl "webkernel::" ./</tt> to modify any file that contains the phrase you&#8217;re trying to replace, rather than search for only <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> files (this was more flexible in my case).</p>
<p><strong>NOTE:</strong> Be sure to check for strings containing the replaced expression in your code. The replacement may end up escaping part of the string, causing errors (e.g. &#8216;webkernel::&#8217; to &#8216;webkernel\&#8217;). Good luck!</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2008%2F12%2F18%2Fphp6-snapshot-namespace-separator-patch-applied%2F&amp;linkname=PHP6%20Snapshot%20%26%238211%3B%20Namespace%20Separator%20Patch%20Applied">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2008/12/18/php6-snapshot-namespace-separator-patch-applied/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Stream Wrapper Irritation</title>
		<link>http://mikegerwitz.com/2008/05/01/php-stream-wrapper-irritation/</link>
		<comments>http://mikegerwitz.com/2008/05/01/php-stream-wrapper-irritation/#comments</comments>
		<pubDate>Thu, 01 May 2008 03:45:42 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/2008/05/01/php-stream-wrapper-irritation/</guid>
		<description><![CDATA[Well; after several wasted hours looking into what I believed to be a bug within the PHP core, I have figured it out. I want to post this in the hope that you, the reader, do not have to go through the stress I went through in attempting to resolve this issue (and almost restorting [...]]]></description>
			<content:encoded><![CDATA[<p>Well; after several wasted hours looking into what I believed to be a bug within the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> core, I have figured it out. I want to post this in the hope that you, the reader, do not have to go through the stress I went through in attempting to resolve this issue (and almost restorting to a standards-defiant workaround).</p>
<p>I speak of creating a <a href="http://us.php.net/manual/en/function.stream-wrapper-register.php">stream wrapper</a> (I&#8217;m creating the datapack wrapper for the <a href="http://www.mycustombb.com/">MyCustomBB</a> project). When you call <a href="http://us.php.net/manual/en/function.ftell.php">ftell()</a> on the handle to the stream, the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> core calls the stream_tell() function within the class defined for that wrapper (in this case, datapack::stream_tell). However, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> <em>only</em> seems to call this function after you use fseek() on your stream. Until then, it caches the file pointer position itself. Okay, seems reasonable. However, unless your stream_seek() function returns a value of TRUE (or any non-false value), <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> ignores the fact that the function was called, and continues to cache it itself.</p>
<p>Okay, so what&#8217;s the problem? If you check the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> documentation for the <a href="http://us.php.net/manual/en/function.fseek.php">fseek()</a> function, you will see the following quote for the return value:</p>
<p><quote>Upon success, returns 0; otherwise, returns -1. Note that seeking past <acronym title="End of file">EOF</acronym> is not considered an error.</quote></p>
<p>Okay; to be complaint with this standard, my stream_seek() function returned a value of zero upon success, and -1 upon failure. Rather, it simply returned the value that was returned by fseek() (<tt>return fseek( $this-&gt;handle, $seek_pos);</tt>). However, 0 = FALSE. Therefore, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> ignored it and would not call stream_tell() until I had stream_seek() return a value of TRUE upon success:</p>
<p><code>return ( fseek( $this-&gt;handle, $seek_pos ) == -1 ) ? FALSE : TRUE;</code></p>
<p>Then, it worked. You can imagine my irritation. However, in the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> documentation, which I did overlook, it states that stream_seek() should indeed return TRUE on success. My bad, but very stupid of the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> developers to do.</p>
<p>And just to think; I almost had to return the position via fseek() and use <tt>fseek( $handle, 0, SEEK_CUR )</tt> in place of ftell().</p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fmikegerwitz.com%2F2008%2F05%2F01%2Fphp-stream-wrapper-irritation%2F&amp;linkname=PHP%20Stream%20Wrapper%20Irritation">Share/Bookmark</a>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/2008/05/01/php-stream-wrapper-irritation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->