0

Possible Duplicate:
When to use StringBuilder?

I am working hard in performance gaining in the Enterprise project. Our solution architect suggesting to convert all string declarions in to stringbuilder If even one append is there to that string variable. for ex: str += str2;

I know, if we are modiying string many times (ex: many appends) can be time consuming to create new string objects. we should go to stringbuilder.

Now Question is -

Creating string builder object (without specifying default size) Vs one time append in to string variablein respect of performance improvement.

Community
  • 1
  • 1
karthik
  • 449
  • 1
  • 8
  • 22
  • 6
    Why don't you run some code and time it? – Jon Feb 16 '12 at 13:49
  • Why you cannot create simple test for StringBuilder and str += str for about 1000000000 items? And calculate time. That's it. you will get an answer. – NoWar Feb 16 '12 at 13:50
  • For a quick (and correct) answer, take a look at http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html – SWeko Feb 16 '12 at 13:54
  • 1
    @DmitryBoyko - hat would be the the wrong answer for "convert all string declarions" – H H Feb 16 '12 at 13:56

1 Answers1

1

There are a number of articles out there that answer this question for you. Here is the first one I came across:

http://www.codeproject.com/Articles/6771/String-Vs-StringBuilder-C

Basically, the answer is that no, you probably shouldn't use stringbuilder for instances where you only append text once or twice. It is really for when you are doing so repeatedly.

IAmTimCorey
  • 16,412
  • 5
  • 39
  • 75