OJ Develops

Thoughts on software development. .NET | C# | Azure

Improving Stringbuilder Performance

18 April 2013

Improving Stringbuilder Performance thumbnail

In one of our projects we had a requirement to produce a .csv file. In the solution I implemented, I used the StringBuilder class to create the contents of the csv.

It worked great, however it took more than one minute to create the file. What was wrong?

After some tinkering, I found that initializing the StringBuilder capacity in the constructor improved the speed. From more than a minute, the process now took under 10 seconds!

public sealed class CSVReport()
{
    private StringBuilder csv;
    private const int ESTIMATED_NUMBER_OF_CHARACTERS_PER_ROW = 400;
    private const int ESTIMATED_NUMBER_OF_ROWS = 3000;

    public CSVReport()
    {
        // Set the string builder capacity for optimization purposes
        csv = new StringBuilder(ESTIMATED_NUMBER_OF_CHARACTERS_PER_ROW * ESTIMATED_NUMBER_OF_ROWS);
    }
}

So remember, to improve StringBuilder performance specially for cases where there are a large number of objects to append, initialize the StringBuilder with the capacity in the constructor.