Comments on: StringBuilder is not always faster – Part 1 of 2 https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/ Chinh's semi-random thoughts on software development, gadgets, and other things. Sat, 07 Feb 2015 06:48:01 +0000 hourly 1 https://wordpress.org/?v=6.9.1 By: Fixed: What's the best string concatenation method using C#? #answer #solution #it | IT Info https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/comment-page-1/#comment-456225 Sat, 07 Feb 2015 06:48:01 +0000 http://vienxu.com/chinh/blog/20070224/stringbuilder-is-not-always-faster/#comment-456225 […] From Chinh Do – StringBuilder is not always faster: […]

]]>
By: How to: What's the best string concatenation method using C#? | SevenNet https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/comment-page-1/#comment-451299 Wed, 17 Dec 2014 01:27:22 +0000 http://vienxu.com/chinh/blog/20070224/stringbuilder-is-not-always-faster/#comment-451299 […] From Chinh Do – StringBuilder is not always faster: […]

]]>
By: Sam https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/comment-page-1/#comment-210390 Tue, 11 Dec 2012 09:07:22 +0000 http://vienxu.com/chinh/blog/20070224/stringbuilder-is-not-always-faster/#comment-210390 Read this article which focuses on the ‘why’ aspect.

http://www.codetails.com/punitganshani/why-is-stringbuilder-faster-in-string-concatenations/20121030

]]>
By: Lelala https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/comment-page-1/#comment-171437 Wed, 15 Aug 2012 13:13:45 +0000 http://vienxu.com/chinh/blog/20070224/stringbuilder-is-not-always-faster/#comment-171437 In the end, it depends on the specific case.
In most cases, that little margin is only in theory relevant – and if that would be important to you, you may be better off with another language/framework.
For enterprise apps, maintainability and quality is much more important than the very last lightspeed seconds, i’d say.
Sure, some webpages may be experience an improvement in their html-output speed, but its always a trade off…

]]>
By: Trevor https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/comment-page-1/#comment-49558 Wed, 08 Jul 2009 00:36:59 +0000 http://vienxu.com/chinh/blog/20070224/stringbuilder-is-not-always-faster/#comment-49558 Use a MemoryProfiler to see which one is better

]]>
By: Chinh Do https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/comment-page-1/#comment-41897 Fri, 24 Apr 2009 11:17:47 +0000 http://vienxu.com/chinh/blog/20070224/stringbuilder-is-not-always-faster/#comment-41897 Marvin: Good question. Thanks. I am curious about it too so I decided to run some measurements again. My result indicates that doing the initialization like you suggested will save some time off of the StringBuilder result. However it was not enough to be faster than the string test. Seems like it would be a good idea to always seed the initial value as part of the construction if you can do it.

Chinh

]]>
By: Marvin https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/comment-page-1/#comment-41703 Thu, 23 Apr 2009 05:38:39 +0000 http://vienxu.com/chinh/blog/20070224/stringbuilder-is-not-always-faster/#comment-41703 ‘###
This block of code took 1484 milliseconds to run on my PC:

for (int i = 0; i <= 1000000; i++)
{
// Concat strings 3 times using StringBuilder
StringBuilder s = new StringBuilder();
s.Append(i.ToString());
s.Append(i.ToString());
s.Append(i.ToString());
}

And this one, using traditional concatenation, took slightly less time (1344 milliseconds):

for (int i = 0; i <= 1000000; i++)
{
// Concat strings 3 times using traditional concatenation
string s = i.ToString();
s = s + i.ToString();
s = s + i.ToString();
}

The above data suggests that StringBuilder only starts to work faster once the number of concatenations exceed 3.
‘###

I noticed in the ‘string’ example you initialize s with the first string. Would StringBuilder be equivalent or faster for 3 concatenations if you initialized stringbuilder also with the first string? e.g. result in 3 lines instead of 4:

StringBuilder s = new StringBuilder(i.ToString(),16);
s.Append(i.ToString());
s.Append(i.ToString());

*Note StringBuilder Initializer includes default size of 16, otherwise it would be reallocating memory with each append since the size would also initialize to 1.

]]>
By: Chinh Do https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/comment-page-1/#comment-41294 Mon, 20 Apr 2009 22:09:00 +0000 http://vienxu.com/chinh/blog/20070224/stringbuilder-is-not-always-faster/#comment-41294 Michael: That makes a lot of sense. Thanks.

]]>
By: Michael https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/comment-page-1/#comment-41287 Mon, 20 Apr 2009 19:12:37 +0000 http://vienxu.com/chinh/blog/20070224/stringbuilder-is-not-always-faster/#comment-41287 If .Net handles String and StringBuilder like Java does, the reason that 3 is the break even point has to do with object creation. With 3 concatenations, each method creates 5 objects (by my count).

StringBuilder:
1 StringBuilder
3 Strings for input
1 String for the final result

Concatenation:
3 Strings for input
1 String for the result of the concatenation of String 2 + 3
1 String for the result of the concatenation of String 1 and the above

]]>
By: 1kHz | anti-keseronokan » Blog Archive » String Concatenation dan Performance https://www.chinhdo.com/20070224/stringbuilder-is-not-always-faster/comment-page-1/#comment-503 Sun, 30 Sep 2007 03:26:07 +0000 http://vienxu.com/chinh/blog/20070224/stringbuilder-is-not-always-faster/#comment-503 […] StringBuilder is not always faster StringBuilder is not always faster – Part 2 […]

]]>