Performance difference between String and StringBuilder

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 5529 red flag

The performance difference between String and StringBuilder in string concatenation.

Performance difference between String and StringBuilder

In this article we will see the performance difference between String and StringBuilder and we will discuss in which scenario String need to used and in which scenario StringBuilder is best. We know that both String and StringBuilder are classes in .NET class library to perform string manipulation operation.

Performance comparison between String and StringBuilder.

We will create simple example to see how String and StringBuilder performs in long string concatenation.  Have a look on below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            String ValueString = String.Empty;
            StringBuilder ValueStringBuilder = new StringBuilder();
 
            //String concatenation operation
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < 1000; i++)
            {
                ValueString = ValueString + "Value";
            }
            sw.Stop();
            Console.WriteLine("For String :-" + sw.ElapsedTicks);
 
            //StringBuilder concatenation operation
            sw.Restart();
            for (int i = 0; i < 1000; i++)
            {
                ValueStringBuilder.Append("Value");
            }
            sw.Stop();
            Console.WriteLine("For StringBuilder :-" + sw.ElapsedTicks);
            Console.ReadLine();
        }
    }
}

Here is output of the program.


We are seeing that large for string concatenation, StringBuilder is much faster than String.  The reason is when we perform string concatenation operation using String then for each and every concatenation one new String object is created in memory and the old copy of string object initialize into new string object. Hence it takes time.

In case of string concatenation using StringBuilder the new copy of StringBuilder object does not get created. So that it takes less time in concatenation operation.

For small string concatenation.

Now, we will try with small string concatenation operation. In this example we will perform string concatenation operation only for 5 times.

using System;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            String ValueString = String.Empty;
            StringBuilder ValueStringBuilder = new StringBuilder();
 
            //String concatenation operation
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < 5; i++)
            {
                ValueString = ValueString + "Value";
            }
            sw.Stop();
            Console.WriteLine("For String :-" + sw.ElapsedTicks);
 
            //StringBuilder concatenation operation
            sw.Restart();
            for (int i = 0; i < 5; i++)
            {
                ValueStringBuilder.Append("Value");
            }
            sw.Stop();
            Console.WriteLine("For StringBuilder :-" + sw.ElapsedTicks);
 
            Console.ReadLine();
        }
    }
}

Here is output of example.


We are seeing that it’s taking same time for both String and StringBuilder for small string concatenation operations.

When to use String and when StringBuilder?

The String and StringBuilder both can be used to perform string concatenation and manipulation purpose.

Generally string is simple and easy to use. So when the need to concatenate string few times(like 5 to 10) then it’s OK to use String.

If there is a need to concatenate string huge number of times then it’s always recommended to use StringBuilder.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)