Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#
Article

String Vs StringBuilder (C#)

Rate me:
Please Sign up or sign in to vote.
3.58/5 (68 votes)
1 May 2004CPOL3 min read 459.7K   32   35
An article on performance of String Vs StringBuilder and better places where each can be used

Introduction

This is my second article regarding performance. Most of the people use string everywhere in their code. Actually when doing string concatenation, do you know what exactly you are doing? It has a big drawback mainly in concatenation which can be overcome by StringBuilder. It will give a vast improvement in performance when you use concatenation of st<code>ring over String.

What is the Exact Difference?

First we will look at what happens when you concatenate two strings. For a rough idea, think like this. In a loop, you are adding few numbers to get a string to give all the numbers.

C#
string returnNumber = "";
for(int i = 0; i<1000; i++)
{ 
    returnNumber = returnNumber + i.ToString();
}

Here we are defining a string called returnNumber and after that, in the loop we are concatenating the old one with the new to get a string. Do you know when we do like that we are assigning it again and again? I mean it's really like assigning 999 new strings!

Actually the concatenation will create a new string returnNumber, with both old returnNumber and i.ToString(). If we think roughly, how will the performance of the code be? Can you imagine it? No one thinks about this when coding.

If we can have something which is to be defined only once and add all the strings into it, what can you say about the performance. That's what StringBuilder does.

C#
StringBuilder returnNumber = new StringBuilder(10000);
for(int i = 0; i<1000; i++)
{ 
    returnNumber.Append(i.ToString());
}

We are creating a StringBuilder of length 10000 in memory where we can add all the strings. This surely won't create a new string each and every time. Actually we are creating a StringBinder, and whenever something is added it will get copied into that memory area. At the end, we can get the string by StringBuilder.ToString(). Here also, it won't create a new string. It will return a string instance that will point to the string inside the StringBuilder. See, how efficient this is?

To explain this in a practical manner, I'm not going to analyze IL code or Optimized JIT compiled code. You can see the differences by running the samples.

Why String? Can't Use StringBinder Everywhere?

No. You can't. When initializing a StringBuilder, you are going down in performance. Also many actions that you do with string can't be done with StringBinder. Actually it is used mostly for situations as explained above. Last week, I showed a person who used StringBuilder to just add two strings together! It's really nonsense. We must really think about the overhead of initialization. In my personal experience, a StringBuilder can be used where more than four or more string concatenations take place. Also if you try to do some other manipulation (like removing a part from the string, replacing a part in the string, etc.), then it's better not to use StringBuilder at those places. This is because we are anyway creating new strings. Another important issue. We must be careful to guess the size of StringBuilder. If the size which we are going to get is more than what is assigned, it must increase the size. This will reduce its performance.

History

  • 19th April, 2004: Article submitted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: StringBuilder is OFTEN not faster Pin
Thomas Freudenberg2-May-04 19:53
Thomas Freudenberg2-May-04 19:53 
GeneralRe: StringBuilder is OFTEN not faster Pin
Brian Delahunty28-Jul-04 23:47
Brian Delahunty28-Jul-04 23:47 
GeneralRe: StringBuilder is OFTEN not faster Pin
Magikos28-Sep-04 8:31
Magikos28-Sep-04 8:31 
GeneralRe: StringBuilder is OFTEN not faster Pin
jmelgaard17-Oct-07 4:17
jmelgaard17-Oct-07 4:17 
QuestionStringBinder? Pin
Thomas Freudenberg20-Apr-04 21:37
Thomas Freudenberg20-Apr-04 21:37 
AnswerRe: StringBinder? Pin
Chester Ragel20-Apr-04 21:51
Chester Ragel20-Apr-04 21:51 
GeneralRe: StringBinder? Pin
Thomas Freudenberg20-Apr-04 22:17
Thomas Freudenberg20-Apr-04 22:17 
GeneralRe: StringBinder? Pin
Ken Beckett (LSI)29-Apr-04 5:51
Ken Beckett (LSI)29-Apr-04 5:51 
D'Oh! | :doh: Chester, the guy was trying to tell you that you have "StringBuilder" spelled incorrectly as "StringBinder" throughout your entire article.
GeneralRe: StringBinder? Pin
Chester Ragel2-May-04 17:47
Chester Ragel2-May-04 17:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.