Friday, September 9, 2016

Using StringBuilder for Performance

High performance of application is implicit requirement, no one states it, and however it’s there and supposed to be taken care of. Here is my first blog on StringBuilder that hopefully help you.

Using StringBuilder is the most recommended way to concatenate large chunks of strings , mostly this happens in loop. It is the best approach to take, and despite this I have seen many developers not to follow this practice.
I have also seen developer’s instantiating StringBuilder in an inefficient ways too. Below are the guidelines for some efficient ways to use StringBuilder & get some performance.
  • Always try to instantiate StringBuilder with some default value or capacity
  • Use in large loops or at places where most of the concatenations are happening
  • Use the same instance of StringBuilder in all methods that construct a single string

Example code:

static void Main(string[] args)
{
    int guessLength = 30;
    StringBuilder sb = new StringBuilder(100 * guessLength); //Give capacity or value as far as possible
    for (int loop = 0; loop < 100; loop++)
    {
        sb.Append(loop).Append(":").AppendLine();
    }
    ConstructMessage(sb);
    ConstructAdditionalMessage(sb);
    Console.WriteLine(sb);
}

static void ConstructMessage(StringBuilder sb)
{
    for (int loop = 0; loop < 5; loop++)
    {
        sb.AppendFormat("{0}:{1}", loop.ToString(CultureInfo.InvariantCulture), Environment.NewLine);
    }

}
static void ConstructAdditionalMessage(StringBuilder sb)
{
    sb.AppendLine();
    for (int loop = 0; loop < 5; loop++)
    {
        sb.AppendFormat("{0}:{1}", loop.ToString(CultureInfo.InvariantCulture), Environment.NewLine);
    }
}


Can you guess why I have used “loop.ToString(CultureInfo.InvariantCulture)” in the above example code?

Related readings:

No comments: