<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: StringBuilder is not always faster - Part 2 of 2</title>
	<link>http://www.chinhdo.com/20070929/stringbuilder-part-2/</link>
	<description>Chinh's not quite random thoughts on software development, .NET, gadgets, and other things.</description>
	<pubDate>Tue, 02 Dec 2008 01:40:24 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3</generator>
		<item>
		<title>By: Sean Finkel</title>
		<link>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-6102</link>
		<dc:creator>Sean Finkel</dc:creator>
		<pubDate>Wed, 19 Nov 2008 20:30:16 +0000</pubDate>
		<guid>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-6102</guid>
		<description>In my own testing, I noticed that placing all the concatenations on one line vastly affected performance. For my tests, I had each operation run x number of time inside a loop. I then had *that* execute x number of times inside another loop. The inner loop (function) did the actual time clocking, while the outer loop was mainly used to execute the specific benchmark x times, and set performance numbers for reporting. 

For a x value of 1000 (1000 function calls, with each function call looping 1000 times) I came up with the conclusion that the + operator (actually, I used "&#38;" in vb.net) was faster than the StringBuilder method. This was with the both functions concatenation all on the same line. When I moved the concatenation to separate lines StringBuilder came out on top. Not by much mind you.

I concatenated the string "Test: " followed by the loop counter variable (integer) nine times, calling the ToString method on it.

These are the averages I got:
Concat with '&#38;' - Same Line: 3.703125ms
Concat with '&#38;' - Separate Line: 4.28125ms
Stringbuilder - Same Line: 4
Stringbuilder - Separate Line: 4.078125</description>
		<content:encoded><![CDATA[<p>In my own testing, I noticed that placing all the concatenations on one line vastly affected performance. For my tests, I had each operation run x number of time inside a loop. I then had *that* execute x number of times inside another loop. The inner loop (function) did the actual time clocking, while the outer loop was mainly used to execute the specific benchmark x times, and set performance numbers for reporting. </p>
<p>For a x value of 1000 (1000 function calls, with each function call looping 1000 times) I came up with the conclusion that the + operator (actually, I used &#8220;&amp;&#8221; in vb.net) was faster than the StringBuilder method. This was with the both functions concatenation all on the same line. When I moved the concatenation to separate lines StringBuilder came out on top. Not by much mind you.</p>
<p>I concatenated the string &#8220;Test: &#8221; followed by the loop counter variable (integer) nine times, calling the ToString method on it.</p>
<p>These are the averages I got:<br />
Concat with &#8216;&amp;&#8217; - Same Line: 3.703125ms<br />
Concat with &#8216;&amp;&#8217; - Separate Line: 4.28125ms<br />
Stringbuilder - Same Line: 4<br />
Stringbuilder - Separate Line: 4.078125</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chinh Do</title>
		<link>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-6011</link>
		<dc:creator>Chinh Do</dc:creator>
		<pubDate>Sat, 15 Nov 2008 00:40:09 +0000</pubDate>
		<guid>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-6011</guid>
		<description>CoDr: Thanks for the comment. I think you may have misunderstood the purpose of my "for loop". The loop itself is not what is being tested... think of it as "testing framework" code. The code I am testing is inside the loop. In these kinds of benchmarks, where things can happen so fast, these loops are used to perform "the test" repeatedly so that we can get a better average result in the end.

If you don't have the for loop, results from one test to the next are likely to vary significantly. That is if your timer is even accurate enough to measure the few microseconds it would take to concatenate those strings.</description>
		<content:encoded><![CDATA[<p>CoDr: Thanks for the comment. I think you may have misunderstood the purpose of my &#8220;for loop&#8221;. The loop itself is not what is being tested&#8230; think of it as &#8220;testing framework&#8221; code. The code I am testing is inside the loop. In these kinds of benchmarks, where things can happen so fast, these loops are used to perform &#8220;the test&#8221; repeatedly so that we can get a better average result in the end.</p>
<p>If you don&#8217;t have the for loop, results from one test to the next are likely to vary significantly. That is if your timer is even accurate enough to measure the few microseconds it would take to concatenate those strings.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CoDr</title>
		<link>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-5974</link>
		<dc:creator>CoDr</dc:creator>
		<pubDate>Wed, 12 Nov 2008 01:39:09 +0000</pubDate>
		<guid>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-5974</guid>
		<description>Hi Chinh,
What you're testing isn't showing apples to apples.

In code, you don't instantiate a million new StringBuilders. StringBuilders are a little expensive up front and the fact that you're instantiating it inside the loop is where the time is lost. 

Instantiation is expensive. That's why the stringbuilder is so much better than traditional "+" concatenation that spans multiple calls.

In a coding project, you only create the StringBuilder once then you can use it to continually, efficiently concatenate your variable string, as needed. Please try pulling that line ("StringBuilder sb = new StringBuilder();") out and check out the results... something like this:

		protected void Page_Load(object sender, EventArgs e)
		{
			int iLoopTo = 30000;

			DateTime dt = DateTime.Now;
			string string1 = "";
			for(int i = 0;i &#60;= iLoopTo;i++)
			{
				string1 = string.Concat(string1, i.ToString());
			}
			TimeSpan loopTS = DateTime.Now.Subtract(dt);
			lblTimestamps.Text += loopTS.TotalMilliseconds + " milliseconds for string.Concat";

			dt = DateTime.Now;
			StringBuilder sb = new StringBuilder();
			for(int i = 0;i &#60;= iLoopTo;i++)
			{
				sb.Append(i.ToString());
			}
			loopTS = DateTime.Now.Subtract(dt);
			lblTimestamps.Text += loopTS.TotalMilliseconds + " milliseconds for StringBuilder";

			dt = DateTime.Now;
			string string2 = "";
			for(int i = 0;i &#60;= iLoopTo;i++)
			{
				string2 = string2 + i.ToString();
			}
			loopTS = DateTime.Now.Subtract(dt);
			lblTimestamps.Text += loopTS.TotalMilliseconds + " milliseconds for string = string +";
		}

My results show a pretty impressive performance difference:

13811.8812 milliseconds for string.Concat
   31.2486 milliseconds for StringBuilder
23561.4444 milliseconds for string = string +

Thanks :-)</description>
		<content:encoded><![CDATA[<p>Hi Chinh,<br />
What you&#8217;re testing isn&#8217;t showing apples to apples.</p>
<p>In code, you don&#8217;t instantiate a million new StringBuilders. StringBuilders are a little expensive up front and the fact that you&#8217;re instantiating it inside the loop is where the time is lost. </p>
<p>Instantiation is expensive. That&#8217;s why the stringbuilder is so much better than traditional &#8220;+&#8221; concatenation that spans multiple calls.</p>
<p>In a coding project, you only create the StringBuilder once then you can use it to continually, efficiently concatenate your variable string, as needed. Please try pulling that line (&#8221;StringBuilder sb = new StringBuilder();&#8221;) out and check out the results&#8230; something like this:</p>
<p>		protected void Page_Load(object sender, EventArgs e)<br />
		{<br />
			int iLoopTo = 30000;</p>
<p>			DateTime dt = DateTime.Now;<br />
			string string1 = &#8220;&#8221;;<br />
			for(int i = 0;i &lt;= iLoopTo;i++)<br />
			{<br />
				string1 = string.Concat(string1, i.ToString());<br />
			}<br />
			TimeSpan loopTS = DateTime.Now.Subtract(dt);<br />
			lblTimestamps.Text += loopTS.TotalMilliseconds + &#8221; milliseconds for string.Concat&#8221;;</p>
<p>			dt = DateTime.Now;<br />
			StringBuilder sb = new StringBuilder();<br />
			for(int i = 0;i &lt;= iLoopTo;i++)<br />
			{<br />
				sb.Append(i.ToString());<br />
			}<br />
			loopTS = DateTime.Now.Subtract(dt);<br />
			lblTimestamps.Text += loopTS.TotalMilliseconds + &#8221; milliseconds for StringBuilder&#8221;;</p>
<p>			dt = DateTime.Now;<br />
			string string2 = &#8220;&#8221;;<br />
			for(int i = 0;i &lt;= iLoopTo;i++)<br />
			{<br />
				string2 = string2 + i.ToString();<br />
			}<br />
			loopTS = DateTime.Now.Subtract(dt);<br />
			lblTimestamps.Text += loopTS.TotalMilliseconds + &#8221; milliseconds for string = string +&#8221;;<br />
		}</p>
<p>My results show a pretty impressive performance difference:</p>
<p>13811.8812 milliseconds for string.Concat<br />
   31.2486 milliseconds for StringBuilder<br />
23561.4444 milliseconds for string = string +</p>
<p>Thanks <img src='http://www.chinhdo.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Armen Ayvazyan</title>
		<link>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-584</link>
		<dc:creator>Armen Ayvazyan</dc:creator>
		<pubDate>Thu, 01 Nov 2007 20:55:18 +0000</pubDate>
		<guid>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-584</guid>
		<description>Chinh:

Tests you did are really good and very detail. They proof that nothing is so obvious and simple. It is nice to see that there is another person in this world who thinks about same kind of crazy things :)

I would like to leave for developers the choice they will make whether to use different techniques based on number of concatenations + length of strings or just to use StringBuilder(capacity) everywhere.</description>
		<content:encoded><![CDATA[<p>Chinh:</p>
<p>Tests you did are really good and very detail. They proof that nothing is so obvious and simple. It is nice to see that there is another person in this world who thinks about same kind of crazy things <img src='http://www.chinhdo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I would like to leave for developers the choice they will make whether to use different techniques based on number of concatenations + length of strings or just to use StringBuilder(capacity) everywhere.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chinh Do</title>
		<link>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-583</link>
		<dc:creator>Chinh Do</dc:creator>
		<pubDate>Thu, 01 Nov 2007 16:40:54 +0000</pubDate>
		<guid>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-583</guid>
		<description>Armen:

I'll have to disagree. The tests I did in this article precisely showed that &lt;b&gt;under some circumstances&lt;/b&gt;, + concatenation is faster than StringBuilder, even when pre-allocated with the final capacity. Look at the first test where the concatenated string size is 10. + concatenations is faster up to 3 concatenations.

I understand that the more memory you allocate, the more work you create for the garbage collector. However, the work that is done by the garbage collector is also taken into account in my tests, as I simply measured the total elapsed time. Whatever work that is done by the GC &lt;b&gt;is reflected&lt;/b&gt; in that total elapsed time.

Re the "for" loop, its purpose is to purely multiply the concatenation multiple times to get a more accurate measurement. It's a typical practice in micro-benchmarks. The operation being measured is inside the loop.</description>
		<content:encoded><![CDATA[<p>Armen:</p>
<p>I&#8217;ll have to disagree. The tests I did in this article precisely showed that <b>under some circumstances</b>, + concatenation is faster than StringBuilder, even when pre-allocated with the final capacity. Look at the first test where the concatenated string size is 10. + concatenations is faster up to 3 concatenations.</p>
<p>I understand that the more memory you allocate, the more work you create for the garbage collector. However, the work that is done by the garbage collector is also taken into account in my tests, as I simply measured the total elapsed time. Whatever work that is done by the GC <b>is reflected</b> in that total elapsed time.</p>
<p>Re the &#8220;for&#8221; loop, its purpose is to purely multiply the concatenation multiple times to get a more accurate measurement. It&#8217;s a typical practice in micro-benchmarks. The operation being measured is inside the loop.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Armen Ayvazyan</title>
		<link>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-582</link>
		<dc:creator>Armen Ayvazyan</dc:creator>
		<pubDate>Thu, 01 Nov 2007 10:57:09 +0000</pubDate>
		<guid>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-582</guid>
		<description>Well, 
StringBuilder with specified capacity is always faster. You measured only first phase of concatenation - memory allocation. Due to fact that strings are immutable each time we are using "+" operator or String.Concat() the new String object will be allocated in memory which in your case you will have 100000 String objects in memory where 99999 are not used at all. 

So as you can guess we are creating additional work for a Garbage Collector and using more memory than it actually could be used.

In case of StringBuilder with specified capacity there will be only 1 object created in memory and *NO* additional work for Garbage Collector.

 “+” operator takes more CPU time, Memory size and creates extra time for a Garbage Collector to clear the mess it built. Unlike StringBuilder with specified parameter saves memory space and CPU time as object will be allocated only ones and GarbageCollector won’t have anything to clean up after. 

My Conclusion is: Use StringBuilder with specified capacity almost always.

http://blog.dotnetstyling.com/archive/2007/10/27/How-fast-SringBuilder-is.aspx</description>
		<content:encoded><![CDATA[<p>Well,<br />
StringBuilder with specified capacity is always faster. You measured only first phase of concatenation - memory allocation. Due to fact that strings are immutable each time we are using &#8220;+&#8221; operator or String.Concat() the new String object will be allocated in memory which in your case you will have 100000 String objects in memory where 99999 are not used at all. </p>
<p>So as you can guess we are creating additional work for a Garbage Collector and using more memory than it actually could be used.</p>
<p>In case of StringBuilder with specified capacity there will be only 1 object created in memory and *NO* additional work for Garbage Collector.</p>
<p> “+” operator takes more CPU time, Memory size and creates extra time for a Garbage Collector to clear the mess it built. Unlike StringBuilder with specified parameter saves memory space and CPU time as object will be allocated only ones and GarbageCollector won’t have anything to clean up after. </p>
<p>My Conclusion is: Use StringBuilder with specified capacity almost always.</p>
<p><a href="http://blog.dotnetstyling.com/archive/2007/10/27/How-fast-SringBuilder-is.aspx" rel="nofollow">http://blog.dotnetstyling.com/archive/2007/10/27/How-fast-SringBuilder-is.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 1kHz &#124; anti-keseronokan &#187; Blog Archive &#187; String Concatenation dan Performance</title>
		<link>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-504</link>
		<dc:creator>1kHz &#124; anti-keseronokan &#187; Blog Archive &#187; String Concatenation dan Performance</dc:creator>
		<pubDate>Sun, 30 Sep 2007 03:26:44 +0000</pubDate>
		<guid>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-504</guid>
		<description>[...] Hari ini, aku terbaca beberapa artikel tentang string concatenation lagi: StringBuilder is not always faster StringBuilder is not always faster - Part 2 [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] Hari ini, aku terbaca beberapa artikel tentang string concatenation lagi: StringBuilder is not always faster StringBuilder is not always faster - Part 2 [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: StringBuilder is not always faster &#187; Chinh Do</title>
		<link>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-500</link>
		<dc:creator>StringBuilder is not always faster &#187; Chinh Do</dc:creator>
		<pubDate>Sat, 29 Sep 2007 06:38:17 +0000</pubDate>
		<guid>http://www.chinhdo.com/20070929/stringbuilder-part-2/#comment-500</guid>
		<description>[...] have posted a follow-up article to provide more detailed analysis and to answer some of the questions asked by [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] have posted a follow-up article to provide more detailed analysis and to answer some of the questions asked by [&#8230;]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
