Saturday 2 June 2012

StringBuilder1 in C#


String concatenation is one of the commonly used operations among programmers. If you don't handle the string concatenation in .NET properly, it may decrease the performance of an application.
You can concatenate strings in two ways -
  • First, traditional way of using string and adding the new string to an existing string. In the .NET Framework, this operation is costly. When you add a string to an existing string, the Framework copies both the existing and new  data to the memory, deletes the existing string, and reads data in a new string. This operation may be very resource consuming in lengthy string concatenation operations.
  • The second and better way to concatenate strings in .NET is using the StringBuilder class. The StringBuidler class provides the Append method, that inserts a new string to an existing string. 
To prove this theory, we will write two routines and both will repeat the same operations. One is using the string class and second is using the StringBuilder class.
Listing 1 shows the routine that uses string class to concatenate strings.
As you can see from Listing 1, I make a loop and concatenate string using the "+" operator. Before I start the loop, I display the start time and when the loop is done, I display the stop time. 
// Put user code to initialize the page here
// Concatenation using string
Console.WriteLine("String routine");
string str = string.Empty;
DateTime startTime = DateTime.Now;
Console.WriteLine("Start time:" + startTime.ToString());
for(int i=0; i<10; i++)
{
str += i.ToString();
}
DateTime stopTime = DateTime.Now;
Console.WriteLine("Stop time:" + stopTime.ToString());

Listing 1
Now let's see the routine that uses the StringBuidler class. As you can see from Listing 2, we do the exact sample operation but this time we use StringBuilder.Append method to concatenate strings.
// Concatenation using StringBuilder
Console.WriteLine("StringBuilder routine");
StringBuilder builder = new StringBuilder();
startTime = DateTime.Now;
Console.WriteLine("Start time:" + startTime.ToString());
for(int i=0; i<10; i++)
{
builder.Append(i.ToString());
}
stopTime = DateTime.Now;
Console.WriteLine("Stop time:" + stopTime.ToString());

Listing 2
Now let's run the application. You may not notice the difference for the  loop from 1 to 10 as you can see from the following figure because the difference is in milliseconds. 

But if you change the loop as following: 

for
(int i=0; i<10000; i++)

You will notice the difference in seconds as you can see from the following Figure.

What about changing loop to the following? 

for(int i=0; i<100000; i++)

Now you will see a big difference. This time the difference is in minutes as you can see from the following figure.

Well, that was just time. Every time you add two strings using + operator, a new string allocation is reserverd in memory and used for this new string. So let's say, you are adding strings 100 times using + operator, there will be 100 new memory allocations. In case of StringBuilder, there will only be one memory allocation.
Recommendation
If you have to concatenate a string more than 10 times, it's better to use StringBuilder.
Sample Code
So be cautious when concatenating strings more than just once. Here is the complete listing of the sample code: 
using System;
using System.Text;
namespace StringBuilderSamp
{
class Class1
{
static void Main()
{
// Concatenation using string
Console.WriteLine("String routine");
string str = string.Empty;
DateTime startTime = DateTime.Now;
Console.WriteLine("Start time:" + startTime.ToString());
for(int i=0; i<100000; i++)
{
str += i.ToString();
}
DateTime stopTime = DateTime.Now;
Console.WriteLine("Stop time:" + stopTime.ToString());
// Concatenation using StringBuilder
Console.WriteLine("StringBuilder routine");
StringBuilder builder = new StringBuilder();
startTime = DateTime.Now;
Console.WriteLine("Start time:" + startTime.ToString());
for(int i=0; i<100000; i++)
{
builder.Append(i.ToString());
}
stopTime = DateTime.Now;
Console.WriteLine("Stop time:" + stopTime.ToString());
}
}
}


No comments:

Post a Comment