Shiny Donkey!
Shiny Donkey! Shiny Donkey! Shiny Donkey!
                  Fake Banner Ads!  Mini-Sites!  
    New Shiny Donkey Posts  more >>
JER
Rob Miller
Rob Miller
satishreddy
* BREAKING NEWS *  9 out of 10 Vegas oddsmakers recommend a crown of thorns instead of shinydonkey.com

.NET StringBuilder vs. String -- to the death!
[reply]   

06/24/04 09:46 AM EST
posted by JER email web

Matt dissed some StringBuilder code I'd written and it got me thinking about just how efficient string concatenation is w/ the various common approaches. I started a thread on microsoft's C# forum yesterday and got some great responses.

 


[reply]   

06/25/04 04:23 AM EST
posted by Matthew email web

I really enjoyed that thread. Learned a lot about strings and c#. There are several things I should do to clerify my position on Stringbuilder. First I was informed that one should always use sb over + by someone I didn't think to question...my first and biggest mistake. Second it was in VB.net...so there may be something about the vb compiler that is not nearly as cool as the c# compiler(I would not be surprised). Having said all that has anybody looked at the speed/cost differences in the following scenario:(it was mentioned in the thread @ microsoft but everyone was caught up on the constant folding.)

str = "thing1" + variableThing + "anotherthing";

vs.

strB.append("thing1");
strB.append(variableThing);
strB.append("anotherthing");

I was also reading some of the links that came off the thread about how you have to understand the pattern of your appends. I personally didn't even recognize that you could initialize the buffersize in a stringbuilder.

So what it really comes down to is: if you are doing lots of constant concats...don't worry constant folding will take care of you. However if you are concatenating something unknown at design time ... work through the patterns and test which will give you the best performance given your set of circumstances.

To sum up...At Best I was misled and at worst I was just plain wrong. Thanks JER for challenging my assumptions.

 


[reply]   

06/25/04 07:25 AM EST
posted by JER email web

Yes, I was misled, too, and told that StringBuilder is always better than "+"-ing a bunch of strings together. I knew from experience that any performance difference would be SO minimal that I often err on the side of readibility over optimal code -- or so I thought!

As for your example question, Matt, the following C# compiles to the following IL:

C# Code
string variableThing = "Michael Moore";

string str = "thing1" + variableThing + "anotherthing";

System.Text.StringBuilder strB = new System.Text.StringBuilder();
strB.Append("thing1");
strB.Append(variableThing);
strB.Append("anotherthing");


IL Code
IL_0000: ldstr "Michael Moore"
IL_0005: stloc.0

IL_0006: ldstr "thing1"
IL_000b: ldloc.0
IL_000c: ldstr "anotherthing"
IL_0011: call string [mscorlib]System.String::Concat(string,
string,
string)
IL_0016: stloc.1

IL_0017: newobj instance void [mscorlib]System.Text.StringBuilder::.ctor()
IL_001c: stloc.2
IL_001d: ldloc.2
IL_001e: ldstr "thing1"
IL_0023: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
IL_0028: pop
IL_0029: ldloc.2
IL_002a: ldloc.0
IL_002b: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
IL_0030: pop
IL_0031: ldloc.2
IL_0032: ldstr "anotherthing"
IL_0037: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
IL_003c: pop



So in my small example, I think the simple string with concatenation is much cleaner than the StringBuilder. Notice that each time StringBuilder has to call "Append" it loads itself first, then the value to add, whereas with the concatenation the loading occurs just once per string. I looked at the IL for Concat & Append but it became obfuscated at a certain point and I wasn't able to gain much insight into their relative expense.

 


[reply]   

06/25/04 07:43 AM EST
posted by Nate

My ignorant understanding of using the Stringbuilder class was that it was more efficient if you were creating an application for text manipulation that was going to be working with lots and lots of text. Like I said though, I'm ignorant.

 


[reply]   

06/27/04 07:22 PM EST
posted by Blearns email web

Odd, I rarely ever do either...

I much more often do a string.Format()

 


[reply]   

06/28/04 09:17 AM EST
posted by Doug2 web

string.Format() rules.

um.. I think StringBuilder is good for something like

while (blah)
sb.Add("X");

things that have an uncertain concatination. I'd have to look back in my O'Reilly book for a more in-depth discussion of it.

 


[reply]   

06/28/04 09:51 AM EST
posted by JER email web

In response to BLK's "I much more often do a string.Format()"

Funny thing about that... string.Format() uses StringBuilder and is the LEAST efficient of any of these approaches. Every overload of string.Format() calls the fattest string.Format overload, which instantiates a StringBuilder and calls StringBuilder.AppendFormat(), which internally makes a bunch of StringBuilder.Append() calls... so we could have just done that in the first place.

That said, using string.Format is just fine, since performance lag is infinitesimal and will probably NEVER cause any problems. Also, string.Format does have a user-friendly syntax that is legible to VB developers and the folks at How's Your News.

But purists will continue to waste hours trying to eek out .004 seconds of performance gain for simple string concatenation.

 


[reply]   

06/28/04 12:17 PM EST
posted by alex email

Um... zing? I think.

 


[reply]   

06/28/04 03:18 PM EST
posted by Blearns email web

Who knew?

Of course those of us in web land don't care. We use all sorts of shortcuts to decrease the number of hours it takes to deliver a solution with total disregard for the amount of time it takes to run the code. At web speeds it's all lightning fast...

 

Name

 registered? log in!

E-mail (optional)

Website (optional)

  

To ensure security, this site requires unregistered users to enter a verification code:
 
Your code is:  
Enter Code:   

Note: Only registered ShinyDonkey.com users can post images. Only administrators can delete images.

  

New messages disabled indefinitely due to SPAM.



"And remember, a shiny new donkey for whoever brings me the head of Colonel Montoya..."
e-mail webmaster