Some folks have said (you've heard them in the halls), “Oh ya, you should NEVER use String Concatenation, dude, use StringBuilder...ALWAYS. Totally.”
Rico weighs in on this, and he's right on (emphasis mine):
I tend to give advice like "Many users find Stringbuilders useful for their concatenation pattens, consider using them and measure to see if they help you". Wussy but safe :) Big string -> small appendsIt's substantially likely that appends will fit in the slop and so they're fast, this is the best case (buffer size becomes double the string when it no longer fits so on average the slop is half the current string length) (if there are lots of small appends to a big string you win the most using stringbuilder)Big string -> big appendsWhile the string is comparable in size (or smaller) to the appends stringbuilder won't save you much, if this continues to the point where the appends are small compared to the accumlated string you're in the good case Small string -> big appendsbad case, string builder will just slow you down until enough slop has built up to hold those appends, you move to "big string big appends" as you append and finally to "big string small appends" if/when the buffer becomes collossalSmall string -> small appendscould be ok if you had a good idea how big your string was going to get and preallocated enough so that you have sufficient slop for the appends. You might be able to do better if you just concated all the small appends together in one operation. It's very hard to say which is faster/smaller in general... it's all about the usage pattern. [Rico Mariani]
I tend to give advice like "Many users find Stringbuilders useful for their concatenation pattens, consider using them and measure to see if they help you". Wussy but safe :)
Big string -> small appendsIt's substantially likely that appends will fit in the slop and so they're fast, this is the best case (buffer size becomes double the string when it no longer fits so on average the slop is half the current string length) (if there are lots of small appends to a big string you win the most using stringbuilder)Big string -> big appendsWhile the string is comparable in size (or smaller) to the appends stringbuilder won't save you much, if this continues to the point where the appends are small compared to the accumlated string you're in the good case Small string -> big appendsbad case, string builder will just slow you down until enough slop has built up to hold those appends, you move to "big string big appends" as you append and finally to "big string small appends" if/when the buffer becomes collossalSmall string -> small appendscould be ok if you had a good idea how big your string was going to get and preallocated enough so that you have sufficient slop for the appends. You might be able to do better if you just concated all the small appends together in one operation.
It's very hard to say which is faster/smaller in general... it's all about the usage pattern. [Rico Mariani]
Scott at DevReach in Bulgaria in October
Developer Stand up Comedy - Coding 4 Fun
TechDays/DevDays Netherlands and Belgium:
Posts by Category Posts by Month
Greatest Hits Dev Tools List