<?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>Bitmatic &#187; string</title>
	<atom:link href="http://bitmatic.com/tag/string/feed" rel="self" type="application/rss+xml" />
	<link>http://bitmatic.com</link>
	<description>Lean IT-solutions in .NET/C#</description>
	<lastBuildDate>Tue, 11 Jan 2011 09:37:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>String &amp; StringBuilder performance in the Compact Framework</title>
		<link>http://bitmatic.com/c/string-stringbuilder-performance-in-the-compact-framework</link>
		<comments>http://bitmatic.com/c/string-stringbuilder-performance-in-the-compact-framework#comments</comments>
		<pubDate>Tue, 28 Jul 2009 17:01:48 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Compact Framework]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://bitmatic.com/?p=395</guid>
		<description><![CDATA[Here&#8217;s a few general guidelines when working with strings: Don&#8217;t concatenate strings &#8211; use a StringBuilder instead Whenever possible initialize the StringBuilder with enough capacity to avoid re-allocations Excessive concatenation of strings will be several orders of magnitude slower than using a StringBuilder String.Format() &#038; StringBuilder.AppendFormat() are significantly slower than just using the +operator or [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a few general guidelines when working with strings:</p>
<ul>
<li>Don&#8217;t concatenate strings &#8211; use a StringBuilder instead</li>
<li>Whenever possible initialize the StringBuilder with enough capacity to avoid re-allocations</li>
<li>Excessive concatenation of strings will be several orders of magnitude slower than using a StringBuilder</li>
<li>String.Format() &#038; StringBuilder.AppendFormat() are significantly slower than just using the +operator or Append() &#8211; but may be more readable</li>
<li>When performance optimizing an application &#8211; string usage is a good place to start</li>
</ul>
<h3>Don&#8217;t concatenate strings</h3>
<p>The <a href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx">StringBuilder</a> is an often overlooked class, that can be used to solve a lot of performance problems in your applications.<br />
To give an example of how ineffective the basic string can be, take a look at the following code that builds 2 large strings by concatenating 1000 times to the same string using String and StringBuilder:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">string</span> s = <span class="kwrd">string</span>.Empty;
<span class="kwrd">for</span> (<span class="kwrd">int</span> j = 0; j &lt; 1000; j++)
{
  s += <span class="str">"Test of string performance."</span>;
}

StringBuilder sb = <span class="kwrd">new</span> StringBuilder();
<span class="kwrd">for</span> (<span class="kwrd">int</span> j = 0; j &lt; 1000; j++)
{
  sb.Append(<span class="str">"Test of string performance."</span>);
}
</pre>
<p>Performance is measured on my HTC S710 moblie phone. The performance difference is very noticeable.</p>
<ul>
<li>The String implementation takes about 800ms</li>
<li>The StringBuilder implementation takes about 12ms</li>
</ul>
<p>StringBuilder is almost 70 times faster than String in this particular example and initializing the StringBuilder with enough capacity up front will shave another 2ms off. I have done tons of optimizations on Compact Framework applications over the years, and the first thing i usually do is replace String with StringBuilder in code similar to the above example.</p>
<h3>Immutable strings &#8211; blessing or curse?</h3>
<p>Strings in the .NET framework are immutable &#8211; meaning that once they are created they can not be changed. It is a design decision from the .NET framework designers, that allows for a lot of optimizations, but also cost performance under certain circumstances.<br />
This is the main reason the String concatenation from before is so slow. Every time the strings are concatenated, a new String is created, and the old one is left for the garbage collector. At least a thousand strings of increasing size are created, copied and garbage collected for no good reason. It is amazingly ineffective.</p>
<h3>StringBuilder &#8211; definitely a blessing!</h3>
<p>The StringBuilder is the thing to use, when handling and manipulating large strings. It holds the string in an internal array that it grows as needed. This enables a very fast append that only rarely allocates anything new. In my test the StringBuilder initially had a capacity of 64 bytes, and grew to double size whenever needed, but implementations may vary.<br />
This behaviour means that you can optimize performance even further by initializing the StringBuilder to a size that is slightly larger than what you expect the final string to be &#8211; thereby removing the need to ever re-allocate.</p>
<h3>A word or two about String.Format()</h3>
<p>I always assumed that using String.Format() to splice strings with variables was the fastest way of doing it. Tests show, however, that it is not&#8230;<br />
I have made test of 4 different ways of accomplishing the same thing:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="rem">//using string</span>
<span class="kwrd">string</span> x = <span class="str">"Test of string performance."</span> + 1 + 2 + <span class="str">"hello"</span>;
<span class="kwrd">string</span> x = <span class="kwrd">string</span>.Format(<span class="str">"Test of string performance.{0}{1}{2}"</span>, 1, 2, <span class="str">"hello"</span>);

<span class="rem">//using StringBuilder</span>
sb.Append(<span class="str">"Test of string performance."</span>).Append(1).Append(2).Append(<span class="str">"hello"</span>);
sb.AppendFormat(<span class="str">"Test of string performance.{0}{1}{2}"</span>, 1, 2, <span class="str">"hello"</span>);</pre>
<p>The results are similar for both the String and StringBuilder. Using the +operator or chained appends is <b>roughly twice as fast</b> as using the Format() method. This came as a bit of a surprise to me.</p>
]]></content:encoded>
			<wfw:commentRss>http://bitmatic.com/c/string-stringbuilder-performance-in-the-compact-framework/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

