When to use StringBuilder? [duplicate]
Asked Answered
T

6

19

Possible Duplicate:
String vs StringBuilder

I just revisited some of the books that I used to pick up VB.NET. I am not sure I've got this in my head, understand how/what StringBuilder is.

What is the guidance for using? Is it best to use it if you are are concatenating 2 strings or 50?

Or when the the total string length is greater than 128 characters?

Or will you see a performance benefit whenever you use it to add strings together?

In which case is it better to use a StringBuilder instance to build a SQL statement than string.format("Select * from x where y = {0}",1)?

It's always struck me that declaring another variable and including a name space is not beneficial for small string concatenations, but I am not sure now.

Sorry, lot of documentation tells you what to use, just not what's best.

Tal answered 9/2, 2009 at 21:13 Comment(1)
Try this question: https://mcmap.net/q/10273/-string-vs-stringbuilderTeirtza
U
40

I've got an article on this very topic. In summary (copied from the bottom of the page):

  • Definitely use StringBuilder when you're concatenating in a non-trivial loop - especially if you don't know for sure (at compile time) how many iterations you'll make through the loop. For example, reading a file a character at a time, building up a string as you go using the += operator is potentially performance suicide.
  • Definitely use the concatenation operator when you can (readably) specify everything which needs to be concatenated in one statement. (If you have an array of things to concatenate, consider calling String.Concat explicitly - or String.Join if you need a delimiter.)
  • Don't be afraid to break literals up into several concatenated bits - the result will be the same. You can aid readability by breaking a long literal into several lines, for instance, with no harm to performance.
  • If you need the intermediate results of the concatenation for something other than feeding the next iteration of concatenation, StringBuilder isn't going to help you. For instance, if you build up a full name from a first name and a last name, and then add a third piece of information (the nickname, maybe) to the end, you'll only benefit from using StringBuilder if you don't need the (first name + last name) string for other purpose (as we do in the example which creates a Person object).
  • If you just have a few concatenations to do, and you really want to do them in separate statements, it doesn't really matter which way you go. Which way is more efficient will depend on the number of concatenations the sizes of string involved, and what order they're concatenated in. If you really believe that piece of code to be a performance bottleneck, profile or benchmark it both ways.
Ursuline answered 9/2, 2009 at 21:16 Comment(3)
Curious, have you ever profiled a StringBuilder vs. String.Concat(theStrings.ToArray())? String.Concat is actually a pretty effecient method.Carbonation
If you already have all the strings you need in an array, then I'd expect String.Concat to be very fast indeed - it can work out (perfectly) the size required to start with. Having to convert the list to an array to start with is unfortunate, mind you. A String.Concat(IList<string>) would be nice.Ursuline
Oh, and in other news: cut and paste for teh win ;)Ursuline
M
14

Here is my rule of thumb:

StringBuilder is best used when the exact number of concatenations is unknown at compile time.

Milford answered 9/2, 2009 at 21:14 Comment(3)
Ok so that would mean that 99% of database applications wouldn't use stringbuilder to build the SQL statement, but sometimes your not sure how many "wheres" you are going to concatenate to the end, will alwys be 1 ... but shouldn't be more than 3, would you still use stringbuilder for this ?Tal
It's a good rule of thumb but occasionally if youre building a very large string, yet use no loops, a StringBuilder would be better.Teirtza
@BC - good point - rules of thumb always have exceptions :)Milford
S
3

Coding Horror has a good article concerning this question, The Sad Tragedy of Micro-Optimization Theater.

Semipostal answered 9/2, 2009 at 21:19 Comment(0)
C
0

Personally I use StringBuilder when I have more than just one or two strings to concatenate. I'm not sure if there's a real performance hit to be gained, but I've always read and been told that doing a regular concatenation with multiple strings creates an extra copy of the string each time you do it, while using StringBuilder keeps one copy until you call the final ToString() method on it.

Cytoplasm answered 9/2, 2009 at 21:17 Comment(0)
C
0

Someone's figured out experimentally that the critical number is 6. More than 6 concatenations in a row and you should use a StringBuilder. Can't remember where I found this.

However, note that if you just write this in a line:

"qwert" + "yuiop" + "asdf" + "gh" + "jkl;" + "zxcv" + "bnm" + ",."

That gets converted into one function call (I don't know how to write it in VB.net)

String.Concat("qwert", "yuiop", "asdf", "gh", "jkl;", "zxcv", "bnm", ",.");

So if you're doing all concatenations on one line, then don't bother with StringBuilder because String.Concat effectively will do all the concatenations in one go. It's only if you're doing them in a loop or successively concatenating.

Calci answered 9/2, 2009 at 21:18 Comment(0)
J
0

My rule - when you're adding to a string in a For or Foreach loop, use the StringBuilder.

Joselow answered 9/2, 2009 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.