Kevin Dente's Blog

The Blip in the Noise
String concatenation - + vs StringBuilder

Countless times I've read that the when doing string concatenation, the StringBuilder class is more efficient than the String "+" operator. I pretty much just accepted that advice until a couple of days ago. But after poking around a bit, it now looks like there's some simple and common cases where in fact the opposite is true.

I got started on this because the existence of the String.Concat method finally entered my consciousness. I'm sure I've seen it before, but I never gave it much thought. Between "+" and StringBuilder, I never had much reason to, I guess. And I never see it used in sample code. When I finally grokked it's existence, I did a little Reflector'ing to look at the method's implementation (the Rotor source works just as well). Lo and behold, the Concat methods that take String arguments use what look to be efficient internal string allocation and copy methods to build up the concatenated string. There are Concat overloads for up to 4 string arguments, so I figured for simple cases I'll would just use String.Concat.

But it got even better. I found this interesting article, which talks about the internals of Strings in the .NET framework. It points out that the compiler converts string operator + calls of up to 4 arguments to a call to String.Concat. So a + b +c gets converted into String.Concat(a, b, c), which avoids the creation intermediate string objects.

With my curiosity piqued, I ran a very quick-and-dirty (and decidedly unscientific) test of 10,000 calls to "a + b + c" vs 10,000 equivalent StringBuilder calls. And in fact, a + b + c was faster. Now, I'm not really that worried about the relative performance of the two methods  - we're talking a few ticks here. But since the + operator (and even String.Concat) can be much more convenient to use than StringBuilder for quick and dirty string concatenation, I'm quite happy that I can use it without worrying about horrible inefficiencies. Obviously if you're building up giant XML strings, StringBuilder is the way to go. But if you're combining 4 or fewer strings, + is just fine.

 

 

 

Published Wednesday, August 27, 2003 12:08 PM by kevindente

Comments

# re: String concatenation - + vs StringBuilder@ Wednesday, August 27, 2003 7:58 PM

I read somewhere on MSDN, maybe a .NET show transcript or something, that said around 10 strings is when the performance of StringBuilder becomes significant over the other methods.

Darrell

# re: String concatenation - + vs StringBuilder@ Thursday, August 28, 2003 10:45 AM

Yeah, I've always gone with the rule of thumb that for anything less than 10 concats the stringbuilder is overkill.

I've also been using String.Format for a lot of string building too. Works fine. And when I need to build a bigger string, .AppendFormat on the StringBuilder is there.

Phil Scott

# <br> UnknownReference@ Friday, August 29, 2003 9:18 AM


UnknownReference

TrackBack

# re: String concatenation - + vs StringBuilder@ Monday, October 20, 2003 7:39 AM

On my blog is a small entry I wrote about making the syntax of StringBuilder slightly easier. Might be of interest. Thanks.

Steve Dunn

# re: String concatenation - + vs StringBuilder@ Friday, November 14, 2003 5:37 AM

The problem here isn't the concatenation of 3 string many times, but the concatenation of many string a few times. I'm not sure, but I believe that in a few string concatenation the '+' could be better.

JC

João Cintra

# re: String Concatenation Speed?@ Thursday, February 19, 2004 6:22 AM

TrackBack

# re: String concatenation - + vs StringBuilder@ Wednesday, April 14, 2004 3:53 PM

Thanks, great post!

Sharbel

# re: String concatenation - + vs StringBuilder@ Tuesday, June 01, 2004 6:12 AM

this is a great site to take help in string concatenation..

hardeep

# re: String concat..... The endless story@ Tuesday, May 24, 2005 7:45 AM

TrackBack

# re: String concatenation - + vs StringBuilder@ Wednesday, October 25, 2006 2:51 PM

Emphysema is a chronic respiratory disease where there is over-inflation

Gogi

# re: String concatenation - + vs StringBuilder@ Sunday, December 31, 2006 6:42 PM

http://zippedurl.com/4y8v3y

<a href="http://zippedurl.com/4y8v3y">ringtone for cingular com</a>

[URL=http://zippedurl.com/4y8v3y]ringtone for cingular com[/URL]

http://zippedurl.com/2r7u9h

<a href="http://zippedurl.com/2r7u9h">download free cingular ringtone</a>

[URL=http://zippedurl.com/2r7u9h]download free cingular ringtone[/URL]

http://zippedurl.com/7b9p7b

<a href="http://zippedurl.com/7b9p7b">cingular wireless ringtone</a>

[URL=http://zippedurl.com/7b9p7b]cingular wireless ringtone[/URL]

http://zippedurl.com/9x5w7i

<a href="http://zippedurl.com/9x5w7i">free cingular mp3 ringtone</a>

[URL=http://zippedurl.com/9x5w7i]free cingular mp3 ringtone[/URL]

http://zippedurl.com/2n6z3t

<a href="http://zippedurl.com/2n6z3t">download cingular ringtone</a>

[URL=http://zippedurl.com/2n6z3t]download cingular ringtone[/URL]

http://zippedurl.com/9u5h8r

<a href="http://zippedurl.com/9u5h8r">free ringtone for cingular phone</a>

[URL=http://zippedurl.com/9u5h8r]free ringtone for cingular phone[/URL]

http://zippedurl.com/2f3r4l

<a href="http://zippedurl.com/2f3r4l">free cingular wireless ringtone</a>

[URL=http://zippedurl.com/2f3r4l]free cingular wireless ringtone[/URL]

http://zippedurl.com/0k9l9t

<a href="http://zippedurl.com/0k9l9t">cingular free real ringtone</a>

[URL=http://zippedurl.com/0k9l9t]cingular free real ringtone[/URL]

http://zippedurl.com/1j9u8d

<a href="http://zippedurl.com/1j9u8d">ringtone for cingular phone</a>

[URL=http://zippedurl.com/1j9u8d]ringtone for cingular phone[/URL]

http://zippedurl.com/0m6x3h

<a href="http://zippedurl.com/0m6x3h">cingular mp3 ringtone</a>

[URL=http://zippedurl.com/0m6x3h]cingular mp3 ringtone[/URL]

http://zippedurl.com/3t4f4i

<a href="http://zippedurl.com/3t4f4i">cingular cell phone ringtone</a>

[URL=http://zippedurl.com/3t4f4i]cingular cell phone ringtone[/URL]

http://zippedurl.com/6n0d2f

<a href="http://zippedurl.com/6n0d2f">free cingular music ringtone</a>

[URL=http://zippedurl.com/6n0d2f]free cingular music ringtone[/URL]

http://zippedurl.com/9h4v7t

<a href="http://zippedurl.com/9h4v7t">free real music cingular ringtone</a>

[URL=http://zippedurl.com/9h4v7t]free real music cingular ringtone[/URL]

http://zippedurl.com/8k0m0c

<a href="http://zippedurl.com/8k0m0c">downloadable ringtone for cingular</a>

[URL=http://zippedurl.com/8k0m0c]downloadable ringtone for cingular[/URL]

http://zippedurl.com/3r6l0c

<a href="http://zippedurl.com/3r6l0c">cingular free go phone ringtone</a>

[URL=http://zippedurl.com/3r6l0c]cingular free go phone ringtone[/URL]

http://zippedurl.com/2j0f8w

<a href="http://zippedurl.com/2j0f8w">free cingular ringtone usa</a>

[URL=http://zippedurl.com/2j0f8w]free cingular ringtone usa[/URL]

http://zippedurl.com/7h4z8g

<a href="http://zippedurl.com/7h4z8g">cingular ringtone coupon</a>

[URL=http://zippedurl.com/7h4z8g]cingular ringtone coupon[/URL]

http://zippedurl.com/7i1e3w

<a href="http://zippedurl.com/7i1e3w">cingular go phone ringtone</a>

[URL=http://zippedurl.com/7i1e3w]cingular go phone ringtone[/URL]

http://zippedurl.com/7d2q3r

<a href="http://zippedurl.com/7d2q3r">cingular media mall ringtone</a>

[URL=http://zippedurl.com/7d2q3r]cingular media mall ringtone[/URL]

http://zippedurl.com/5p0e3n

<a href="http://zippedurl.com/5p0e3n">cingular phone free real ringtone</a>

[URL=http://zippedurl.com/5p0e3n]cingular phone free real ringtone[/URL]

http://zippedurl.com/4r3w5h

<a href="http://zippedurl.com/4r3w5h">cingular shop ringtone</a>

[URL=http://zippedurl.com/4r3w5h]cingular shop ringtone[/URL]

http://zippedurl.com/5g4u3a

<a href="http://zippedurl.com/5g4u3a">8125 cingular ringtone</a>

[URL=http://zippedurl.com/5g4u3a]8125 cingular ringtone[/URL]

http://zippedurl.com/2w6d7f

<a href="http://zippedurl.com/2w6d7f">christmas cingular ringtone</a>

[URL=http://zippedurl.com/2w6d7f]christmas cingular ringtone[/URL]

4y8v3yww

# re: String concatenation - + vs StringBuilder@ Sunday, September 16, 2007 2:15 PM

Some more data to extract conclusions out: blog.cumps.be/string-concatenation-vs-memory-allocation

The memory usage for various concatenation methods.

CumpsD

# Lexapro weight loss.@ Tuesday, July 08, 2008 4:23 AM

Lexapro. Lexapro and side effects. Lexapro weight loss. Lexapro and drinking.

Side effects of drug lexapro.

Leave a Comment

(required) 
(required) 
(optional)
(required)