<?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 Manipulation</title>
	<atom:link href="http://www.enusbaum.com/blog/tag/c-string-manipulation/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>Tue, 18 Oct 2011 20:22:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>.NET StringBuilder &#8212; Fast, but not as fast as you think!</title>
		<link>http://www.enusbaum.com/blog/2009/05/net-stringbuilder-fast-but-not-as-fast-as-you-think/</link>
		<comments>http://www.enusbaum.com/blog/2009/05/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" 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" 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" 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" 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/" 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>
<div class="su-linkbox" id="post-294-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2009/05/net-stringbuilder-fast-but-not-as-fast-as-you-think/&quot;&gt;.NET StringBuilder &#8212; Fast, but not as fast as you think!&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2009/05/net-stringbuilder-fast-but-not-as-fast-as-you-think/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

