<?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>Intrepid Blog &#187; string</title>
	<atom:link href="http://blog.affien.com/archives/tag/string/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.affien.com</link>
	<description>A few thoughts</description>
	<lastBuildDate>Mon, 01 Mar 2010 00:58:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Stupid PHP (1) (Strings are faster than Arrays)</title>
		<link>http://blog.affien.com/archives/2007/09/23/stupid-php-1-strings-are-faster-than-arrays/</link>
		<comments>http://blog.affien.com/archives/2007/09/23/stupid-php-1-strings-are-faster-than-arrays/#comments</comments>
		<pubDate>Sun, 23 Sep 2007 17:05:14 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[Web development]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[concat]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[stupid]]></category>

		<guid isPermaLink="false">http://blog.w-nz.com/archives/2007/09/23/stupid-php-1-strings-are-faster-than-arrays/</guid>
		<description><![CDATA[When I slowly build a big string out of little bits, the worst thing to do in most languages is to just use string concatenation:
for(something) {
&#160;str += little_bit;
}
Why? Everytime a little bit is added to str, there must be a new string allocated big enough to contain str and the new little bit. Then the [...]]]></description>
			<content:encoded><![CDATA[<p>When I slowly build a big string out of little bits, the worst thing to do in most languages is to just use string concatenation:</p>
<p><code>for(something) {<br />
&nbsp;str += little_bit;<br />
}</code></p>
<p>Why? Everytime a little bit is added to <code>str</code>, there must be a new string allocated big enough to contain <code>str</code> and the new little bit. Then the actual <code>str</code> must be copied in. In most languages there are constructs to efficiently build strings like this instead of concatenating. StringBuffer in C#. StringIO in Python.</p>
<p>But no, PHP has to be stupid. There is no nice construct and you&#8217;ll end up using concatenation. So, I thought to be smart and make use of PHP array&#8217;s and <code>implode</code>. Arrays are here for having elements added and removed all the time so they are properly buffered and should be great at having lots of small elements added. And when I want to pack it all into one big string, I can use PHP&#8217;s builtin <code>implode</code> function.</p>
<p>I wanted to try it out and created two scripts: <code>a.php</code> concats a little (10byte) string one million times and <code>b.php</code> appends it to an array and then <code>implode</code>s it. And because I&#8217;m also interested in the performance of <code>implode</code> I got a script <code>c.php</code> that&#8217;s identical to <code>b.php</code> but doesn&#8217;t implode afterwards. These are the results:</p>
<table>
<tr>
<th>a.php (concat)</th>
<td>0.320s</td>
</tr>
<tr>
<th>b.php (array append and implode)</th>
<td>0.814s</td>
</tr>
<tr>
<th>c.php (array append)</th>
<td>0.732s</td>
</tr>
</table>
<p>Indeed, string concatenation with all its allocation and copying is actually faster than plain simple array appending. PHP is stupid.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2007/09/23/stupid-php-1-strings-are-faster-than-arrays/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
