<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: PHP array_merge is Slow</title>
	<link>http://www.86db1.com/php-array_merge-is-slow</link>
	<description>IBM RPG and Databases</description>
	<pubDate>Thu, 09 Sep 2010 20:10:16 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
		<item>
		<title>By: Bill</title>
		<link>http://www.86db1.com/php-array_merge-is-slow#comment-272</link>
		<dc:creator>Bill</dc:creator>
		<pubDate>Thu, 04 Sep 2008 18:52:43 +0000</pubDate>
		<guid>http://www.86db1.com/php-array_merge-is-slow#comment-272</guid>
		<description>&lt;p&gt;Interesting, however, your homegrown version isn’t doing everything the built in version is. &lt;/p&gt;
&lt;p&gt;From the manual entry for  array_merge:&lt;/p&gt;
&lt;p&gt;If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.&lt;/p&gt;
&lt;p&gt;So its really doing something like following:&lt;/p&gt;
&lt;p&gt;foreach($ar2 as $key=&gt;$i) {
   if( is_numeric($key)) {
      $ar1[] = $i;
   } else {
      $ar1[$key] = $i;
   }
}&lt;/p&gt;
&lt;p&gt;Its probably that plus the time needed to create another whole array by piece.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Interesting, however, your homegrown version isn’t doing everything the built in version is. </p>
<p>From the manual entry for  array_merge:</p>
<p>If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.</p>
<p>So its really doing something like following:</p>
<p>foreach($ar2 as $key=>$i) {<br />
   if( is_numeric($key)) {<br />
      $ar1[] = $i;<br />
   } else {<br />
      $ar1[$key] = $i;<br />
   }<br />
}</p>
<p>Its probably that plus the time needed to create another whole array by piece.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carsten</title>
		<link>http://www.86db1.com/php-array_merge-is-slow#comment-271</link>
		<dc:creator>Carsten</dc:creator>
		<pubDate>Thu, 04 Sep 2008 15:03:15 +0000</pubDate>
		<guid>http://www.86db1.com/php-array_merge-is-slow#comment-271</guid>
		<description>&lt;p&gt;Bill, thanks for the comment. I agree that the builtin version is more advanced in many ways. Still, we’re talking compiled C code vs. interpreted PHP, this is why I’m surprised that there’s such a difference with a relatively low number of key/value pairs. Also, in my experiment both arrays are indeed indexed by numbers, not strings.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Bill, thanks for the comment. I agree that the builtin version is more advanced in many ways. Still, we’re talking compiled C code vs. interpreted PHP, this is why I’m surprised that there’s such a difference with a relatively low number of key/value pairs. Also, in my experiment both arrays are indeed indexed by numbers, not strings.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: joel</title>
		<link>http://www.86db1.com/php-array_merge-is-slow#comment-270</link>
		<dc:creator>joel</dc:creator>
		<pubDate>Thu, 04 Sep 2008 05:24:29 +0000</pubDate>
		<guid>http://www.86db1.com/php-array_merge-is-slow#comment-270</guid>
		<description>&lt;p&gt;That is interesting, but not too unexpected.&lt;/p&gt;
&lt;p&gt;Depending on whether keys are important or not, you’ll find that:
$ar1 += $ar2;
is actually the fastest!&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>That is interesting, but not too unexpected.</p>
<p>Depending on whether keys are important or not, you’ll find that:<br />
$ar1 += $ar2;<br />
is actually the fastest!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carsten</title>
		<link>http://www.86db1.com/php-array_merge-is-slow#comment-269</link>
		<dc:creator>Carsten</dc:creator>
		<pubDate>Thu, 04 Sep 2008 04:25:09 +0000</pubDate>
		<guid>http://www.86db1.com/php-array_merge-is-slow#comment-269</guid>
		<description>&lt;p&gt;Joel, while keys aren’t important, they are duplicated between the two arrays (they are numerical). Using += nukes all the duplicated values, which is not feasible in this context.&lt;/p&gt;
&lt;p&gt;But += is very fast indeed! Thanks for the pointer.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Joel, while keys aren’t important, they are duplicated between the two arrays (they are numerical). Using += nukes all the duplicated values, which is not feasible in this context.</p>
<p>But += is very fast indeed! Thanks for the pointer.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill</title>
		<link>http://www.86db1.com/php-array_merge-is-slow#comment-268</link>
		<dc:creator>Bill</dc:creator>
		<pubDate>Wed, 03 Sep 2008 19:28:54 +0000</pubDate>
		<guid>http://www.86db1.com/php-array_merge-is-slow#comment-268</guid>
		<description>&lt;p&gt;The compiled c code still has to deal with the php data structures in the same manner php code does. Php arrays are not very space or time efficient, so the more complex things you do with them the worse off you are. It would be interesting to do exactly what array_merge was doing in php and measure the time difference versus the native function call. Php should still be slower, as long as you aren’t using the Zend optimizer.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The compiled c code still has to deal with the php data structures in the same manner php code does. Php arrays are not very space or time efficient, so the more complex things you do with them the worse off you are. It would be interesting to do exactly what array_merge was doing in php and measure the time difference versus the native function call. Php should still be slower, as long as you aren’t using the Zend optimizer.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joao Prado Maia</title>
		<link>http://www.86db1.com/php-array_merge-is-slow#comment-267</link>
		<dc:creator>Joao Prado Maia</dc:creator>
		<pubDate>Wed, 03 Sep 2008 18:52:14 +0000</pubDate>
		<guid>http://www.86db1.com/php-array_merge-is-slow#comment-267</guid>
		<description>&lt;p&gt;Carsten,&lt;/p&gt;
&lt;p&gt;So you are doing PHP work now? That’s really cool!&lt;/p&gt;
&lt;p&gt;Long time no see!&lt;/p&gt;
&lt;p&gt;–Joao&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Carsten,</p>
<p>So you are doing PHP work now? That’s really cool!</p>
<p>Long time no see!</p>
<p>–Joao</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: troelskn</title>
		<link>http://www.86db1.com/php-array_merge-is-slow#comment-266</link>
		<dc:creator>troelskn</dc:creator>
		<pubDate>Wed, 03 Sep 2008 13:52:18 +0000</pubDate>
		<guid>http://www.86db1.com/php-array_merge-is-slow#comment-266</guid>
		<description>&lt;p&gt;If the array, you’re iterating over, is a vector (and not a hash), then foreach is the slow way to do it. The fast way would be:
for ($ii=0,$ll=count($ar2); $ii &lt; $ll; ++$ii) {
 $ar1[] = $ar2[$ii];
}
(Also, note ++$ii instead of $ii++, which is a bit faster in PHP).&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>If the array, you’re iterating over, is a vector (and not a hash), then foreach is the slow way to do it. The fast way would be:<br />
for ($ii=0,$ll=count($ar2); $ii < $ll; ++$ii) {<br />
 $ar1[] = $ar2[$ii];<br />
}<br />
(Also, note ++$ii instead of $ii++, which is a bit faster in PHP).</p></p>
]]></content:encoded>
	</item>
</channel>
</rss>
