Jevgeni Borozna's blog

SharePoint, ASP .NET

Appending string in C#: String += vs string.Format vs StringBuilder

Appending strings performance test

String += creates new instance of object and saves it in memory, as a result you have 2 string objects in a memory. For this kind of targets .NET have class named StringBuilder which will work 2500 times faster than string += or trick with string.Format (which is a very bad idea). StringBuilder holds all data in one memory instance and changes it, not creates a new instance each time as it do += for string.

So better practice for appending strings is to use StringBuilder.

C# code:

int iterations = 100000;

 

string testString = string.Empty;

 

// string += test (using string.Concat will return the same result)

Stopwatch sw = new Stopwatch();

sw.Start();

 

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

{

    testString += i.ToString();

}

 

sw.Stop();

 

Console.WriteLine(sw.ElapsedMilliseconds);

 

sw.Reset();

 

// string.Format test

testString = string.Empty;

sw.Start();

 

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

{

    testString = string.Format("{0}{1}", testString, i.ToString());

}

 

sw.Stop();

 

Console.WriteLine(sw.ElapsedMilliseconds);

 

sw.Reset();

 

// StringBuilder test

StringBuilder sb = new StringBuilder();

sw.Start();

 

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

{

    sb.Append(i.ToString());

}

 

testString = sb.ToString();

 

sw.Stop();

 

Console.WriteLine(sw.ElapsedMilliseconds);

 

Console.ReadKey();

Results:

  1. string += appending : 176 654 ms
  2. string.Format appending : 456 265 ms
  3. StringBuilder.Append() :  68 ms
Posted: Dec 11 2009, 03:35 PM by Jevgeni Borozna | with 6 comment(s)
Filed under: ,

Comments

Twitter Trackbacks for Appending string in C#: String += vs string.Format vs StringBuilder - Jevgeni Borozna's blog [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Appending string in C#: String += vs string.Format vs StringBuilder - Jevgeni Borozna's blog         [asp.net]        on Topsy.com

# December 11, 2009 11:13 AM

paul.vencill said:

Interesting but not relevant, imo, unless you're doing a crapload of operations like this.  The average app won't take a hit, especially if you're working in a live environment where there's network latencies etc to worry about that far outshadow a ms-scaled string operation.

# December 11, 2009 12:21 PM

Jevgeni Borozna said:

It is very important when you parsing text files and putting some information together for output file. Rearranging data style for example.

# December 11, 2009 12:24 PM

Ruud van Falier said:

What about String.Concat() ?

I tend to use that one alot.

/Ruud

# December 11, 2009 2:16 PM

Joe Chung said:

Using + to concatenate strings uses string.Concat internally.

# December 11, 2009 2:42 PM

Michael Wallasch said:

Thanks for the info. Regardless of the importance aspect, imho its a nice little piece in respect of  a well implemented application.

# December 11, 2009 5:15 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)