Pages

Thursday, December 22, 2005

String Vs StringBuilder : Kaushal Patel

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 your doing? It has a big drawback mainly in concatenation which can be overcome by StringBuilder. It will give vast improvement in performance when you use concatenation of string 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.

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 its 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 be the performance of the code? Can you imagine it? No one things about this when coding. If we can have something which are be defined only once and add all the strings into it, what can you say about the performance. That's what StringBuilder is doing.
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. Which surely wont create a new string each and every time. Actually we are creating a StringBinder, where whenever something added it will get copied in to that memory area. At the end we can get the string by StringBuilder.ToString(). Here also it wont 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 with some practical I'm not going to analyze IL code or Optimized JIT compiled code. You can see the different 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 show a person, who used StringBuilder to just add two strings together! its 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 concatenation 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, etc) then better not to use StringBuilder at those places. Because anyway we are 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 assigned, it must increase the size. Which will reduce the performance of it.