<?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>Think In Geek &#187; Java</title>
	<atom:link href="http://thinkingeek.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkingeek.com</link>
	<description>In geek we trust</description>
	<lastBuildDate>Sat, 19 Jun 2010 22:00:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>C# and the StringBuilder class</title>
		<link>http://thinkingeek.com/2008/07/16/c-and-the-stringbuilder-class/</link>
		<comments>http://thinkingeek.com/2008/07/16/c-and-the-stringbuilder-class/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 17:07:37 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming tips]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=67</guid>
		<description><![CDATA[This morning I was working on a project at work. It&#8217;s a Web Application using the ASP .NET 2.0 framework and C# as a code behind language. My friend Ioannis came over to see what was I doing and when he saw I was appending some strings together he asked me this question: &#8220;are you [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I was working on a project at work. It&#8217;s a Web Application using the ASP .NET 2.0 framework and C# as a code behind language. My friend Ioannis came over to see what was I doing and when he saw I was appending some strings together he asked me this question: &#8220;<em>are you using a StringBuilder to use those strings?</em>&#8220;. And I replied with this answer: &#8220;<em>no, I am not</em>&#8220;. This kind of stupid dialog came over because last week we were discussing about using StringBuilders instead of the default String class operators to append strings each other in Java. It seemed using the StringBuilder class resulted in an overall performance gain. It was then when I asked: &#8220;<em>don&#8217;t tell me this happens with C#, too?</em>&#8220;. And he answered: &#8220;<em>yes, it does!</em>&#8220;.</p>
<p>So, what&#8217;s the matter with StringBuilders in C#?</p>
<p><span id="more-67"></span></p>
<p>It seems the same thing happens with Strings, C# and Java. Here is a copy paste from the <a href="http://msdn2.microsoft.com/en-us/library/2839d5h5%28vs.80%29.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn2.microsoft.com/en-us/library/2839d5h5_28vs.80_29.aspx?referer=');">MSDN web page</a> about the StringBuilder usage:</p>
<blockquote><p>
The <strong>String</strong> object is immutable. Every time you use one of the methods in the <strong>System.String</strong> class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new <strong>String</strong> object can be costly. The <a href="http://msdn2.microsoft.com/en-us/library/system.text.stringbuilder%28VS.80%29.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn2.microsoft.com/en-us/library/system.text.stringbuilder_28VS.80_29.aspx?referer=');">System.Text.StringBuilder</a> class can be used when you want to modify a string without creating a new object. For example, using the <strong>StringBuilder</strong> class can boost performance when concatenating many strings together in a loop.
</p></blockquote>
<p>
So I decided to run some tests to see how much it was worth to change my code to use the StringBuilder class, because I use the string &#8216;+&#8217; operator a lot in my program, and the results are simply amazing. Here you can find a chart comparing the times it took to concatenate a given number of strings using both methods. See for yourself and then&#8230; use StringBuilders from now on!
</p>
<div id="attachment_68" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/string.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/string-300x180.png" alt="String usage" title="String usage" width="300" height="180" class="size-medium wp-image-68" /></a><p class="wp-caption-text">String usage</p></div>
<div id="attachment_69" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/stringbuilder.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/stringbuilder-300x180.png" alt="StringBuilder usage" title="StringBuilder usage" width="300" height="180" class="size-medium wp-image-69" /></a><p class="wp-caption-text">StringBuilder usage</p></div>
<p>
Here&#8217;s the source code i used if you want to try it for yourself:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Adapdev.Diagnostics</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> SBuilder
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">class</span> Program
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            HiPerfTimer timer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HiPerfTimer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                StringBuilder sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                timer.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1000</span> <span style="color: #008000;">&lt;</span>strong<span style="color: #008000;">&gt;</span> i<span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>j<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                timer.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">double</span> timeStringBuilder <span style="color: #008000;">=</span> timer.<span style="color: #0000FF;">Duration</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #FF0000;">string</span> s <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
                timer.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1000</span> <span style="color: #008000;">&lt;/</span>strong<span style="color: #008000;">&gt;</span> i<span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    s <span style="color: #008000;">+=</span> j<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                timer.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">double</span> timeString <span style="color: #008000;">=</span> timer.<span style="color: #0000FF;">Duration</span><span style="color: #008000;">;</span>
&nbsp;
                StringBuilder line <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                line.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1000</span> <span style="color: #008000;">*</span> i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                line.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                line.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>timeStringBuilder<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                line.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                line.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>timeString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Console</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>line.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
You can download the external timer classes from <a href="http://www.codeproject.com/csharp/highperformancetimercshar.asp" onclick="pageTracker._trackPageview('/outgoing/www.codeproject.com/csharp/highperformancetimercshar.asp?referer=');">The Code Project</a>.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fthinkingeek.com%2F2008%2F07%2F16%2Fc-and-the-stringbuilder-class%2F&amp;linkname=C%23%20and%20the%20StringBuilder%20class" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fthinkingeek.com_2F2008_2F07_2F16_2Fc-and-the-stringbuilder-class_2F_amp_linkname=C_23_20and_20the_20StringBuilder_20class&amp;referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2008/07/16/c-and-the-stringbuilder-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
