This morning I was working on a project at work. It's a Web Application using the ASP .NET 2.0 framework and C# as a code behind language. My friend Ioannis came over to see what was I doing and when he saw I was appending some strings together he asked me this question: "are you using a StringBuilder to use those strings?". And I replied with this answer: "no, I am not". This kind of stupid dialog came over because last week we were discussing about using StringBuilders instead of the default String class operators to append strings each other in Java. It seemed using the StringBuilder class resulted in an overall performance gain. It was then when I asked: "don't tell me this happens with C#, too?". And he answered: "yes, it does!".

So, what's the matter with StringBuilders in C#?

It seems the same thing happens with Strings, C# and Java. Here is a copy paste from the MSDN web page about the StringBuilder usage:

The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.

So I decided to run some tests to see how much it was worth to change my code to use the StringBuilder class, because I use the string '+' operator a lot in my program, and the results are simply amazing. Here you can find a chart comparing the times it took to concatenate a given number of strings using both methods. See for yourself and then... use StringBuilders from now on!

String usage

StringBuilder usage

Here's the source code i used if you want to try it for yourself:

using System;
using System.Collections.Generic;
using System.Text;

using Adapdev.Diagnostics;

namespace SBuilder
{
    class Program
    {
        static void Main(string[] args)
        {
            HiPerfTimer timer = new HiPerfTimer();
            for (int i = 0; i < 100; i++)
            {
                StringBuilder sb = new StringBuilder();
                timer.Start();
                for (int j = 0; j < 1000 <strong> i; j++)
                {
                    sb.Append(j);
                }
                timer.Stop();
                double timeStringBuilder = timer.Duration;

                string s = string.Empty;
                timer.Start();
                for (int j = 0; j < 1000 </strong> i; j++)
                {
                    s += j;
                }
                timer.Stop();
                double timeString = timer.Duration;

                StringBuilder line = new StringBuilder();
                line.Append(1000 * i);
                line.Append(";");
                line.Append(timeStringBuilder);
                line.Append(";");
                line.Append(timeString);
                System.Console.WriteLine(line.ToString());
            }
        }
    }
}

You can download the external timer classes from The Code Project.