<?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>All Things IT Blog &#187; C# String Concatenation</title>
	<atom:link href="http://www.enusbaum.com/blog/tag/c-string-concatenation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.enusbaum.com/blog</link>
	<description>My little nerded out corner of the Internets!</description>
	<lastBuildDate>Thu, 02 Sep 2010 01:43:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>.NET StringBuilder &#8212; Fast, but not as fast as you think!</title>
		<link>http://www.enusbaum.com/blog/2009/05/28/net-stringbuilder-fast-but-not-as-fast-as-you-think/</link>
		<comments>http://www.enusbaum.com/blog/2009/05/28/net-stringbuilder-fast-but-not-as-fast-as-you-think/#comments</comments>
		<pubDate>Thu, 28 May 2009 20:18:47 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[C# Programming]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Microsoft .NET 3.0 / WinFX]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[.NET Optimization]]></category>
		<category><![CDATA[.NET Profiler]]></category>
		<category><![CDATA[.NET Profiling]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# Optimization]]></category>
		<category><![CDATA[C# String Concatenation]]></category>
		<category><![CDATA[C# String Manipulation]]></category>
		<category><![CDATA[C# Strings]]></category>
		<category><![CDATA[StringBuilder]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/?p=294</guid>
		<description><![CDATA[StringBuilder: Friend or Foe?]]></description>
			<content:encoded><![CDATA[<p>I recently ran into a situation where I was tasked to profile some .NET code and do some optimizations anywhere hot spots popped up. I was amazed to find out that one of the BIGGEST offenders in our code block was a simple call to <strong>StringBuilder.Append(char)</strong>. I had to take a step back and scratch my head and wonder if my profiler was confused.</p>
<p>I re-ran some tests using the <strong>StopWatch</strong> class to hard code some metrics into the application and they also confirmed the findings. What&#8217;s up? How could a class that everyone says you can use to your hearts content when it came to string concatenation was failing me?</p>
<p>Turns out, it was a mix of misuse and a common misconception about the <a title="MSDN Documentation -- StringBuilder Class" href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.text.stringbuilder.aspx','MSDN+Documentation+--+StringBuilder+Class')" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.text.stringbuilder.aspx','MSDN+Documentation+--+StringBuilder+Class')" target="_blank">StringBuilder Class</a>.</p>
<p><span id="more-294"></span></p>
<p>One of the first things you learn while picking up .NET is that the <a title="MSDN Documentation -- StringBuilder Class" href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.text.stringbuilder.aspx','MSDN+Documentation+--+StringBuilder+Class')" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.text.stringbuilder.aspx','MSDN+Documentation+--+StringBuilder+Class')" target="_blank">StringBuilder Class</a> is your friend when it comes to concatenating large strings in memory. It beats the pants off of <a title="MSDN Documentation -- String.Concat Method" href="http://msdn.microsoft.com/en-us/library/system.string.concat.aspx" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.string.concat.aspx','MSDN+Documentation+--+String.Concat+Method')" target="_blank">String.Concat</a> and <a title="MSDN Documentation -- String.Format Method" href="http://msdn.microsoft.com/en-us/library/system.string.format.aspx" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.string.format.aspx','MSDN+Documentation+--+String.Format+Method')" target="_blank">String.Format</a>, while also being a mutable object in the Framework utilizing an in-memory buffer.</p>
<p>I used <a title="Homepage -- JetBrains dotTrace Profiler" href="http://www.jetbrains.com/profiler/" onclick="return TrackClick('http%3A%2F%2Fwww.jetbrains.com%2Fprofiler%2F','Homepage+--+JetBrains+dotTrace+Profiler')" target="_blank">JetBrains dotTrace</a> to help profile the application and it was very evident from the get-go that StringBuilder was causing the whole process to slow down.</p>
<p>The nature of my application was basically reading in a text buffer 1 character as a time, and using the <strong>StringBuilder</strong> as an output buffer. So for a 1k file, The method <strong>Append(char)</strong> would be called 1024 times. A 600k file would call <strong>Append(char)</strong> 614,400 times.</p>
<p>So why was I getting burned in execution time? The issue turned out to be two fold.</p>
<p>First, there&#8217;s overhead cost to the call. I don&#8217;t care how lightweight your method is, if you&#8217;re calling it <span style="text-decoration: underline;"><strong>SIX HUNDRED THOUSAND TIMES</strong></span>, it&#8217;s going to take a bit. Let alone a method who handles a string buffer in memory and string manipulation. So basically, no matter how fast StringBuilder actually is, it&#8217;s not a free call and you should consider the fact that the call still has overhead when architecting your solution.</p>
<p>Architecture brings me to my second point. While writing each character individually made sense initally, it seems that it was just lazy <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  The optimized route would have been calling Append with a SUBSTRING of the input buffer, this way we avoid the overhead of multiple calls by writing all the neccisary data in one big blob.</p>
<p>So 600,000 calls to <strong>StringBuilder.Append(char)</strong> becomes only a few hundred calls to <strong>StringBuilder.Append(string.Substring(start, count))</strong>. Sure, the Substring Virtual Method itself has overhead, but it&#8217;s still less than the thousands of calls to <strong>Append(char)</strong> that we&#8217;re saving ourself <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Conclusion?</p>
<p>StringBuilder is fast, but it&#8217;s not free. Take this into consideration when utilizing it while appending large data sets in small chunks. <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Cheers!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2009%2F05%2F28%2Fnet-stringbuilder-fast-but-not-as-fast-as-you-think%2F&amp;title=.NET+StringBuilder+%26%238212%3B+Fast%2C+but+not+as+fast+as+you+think%21" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2009%2F05%2F28%2Fnet-stringbuilder-fast-but-not-as-fast-as-you-think%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2009%2F05%2F28%2Fnet-stringbuilder-fast-but-not-as-fast-as-you-think%2F&amp;title=.NET+StringBuilder+%26%238212%3B+Fast%2C+but+not+as+fast+as+you+think%21" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+.NET+StringBuilder+%26%238212%3B+Fast%2C+but+not+as+fast+as+you+think%21+@+http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2009%2F05%2F28%2Fnet-stringbuilder-fast-but-not-as-fast-as-you-think%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2009/05/28/net-stringbuilder-fast-but-not-as-fast-as-you-think/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
