Microsoft KB Archive/306822

= How to improve string concatenation performance in Visual C# =

Article ID: 306822

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q306822





For a Microsoft Visual JScript .NET version of this article, see 306823.

For a Microsoft Visual Basic .NET version of this article, see 306821.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Text

IN THIS TASK

 * SUMMARY
 * Description of Strings in the .NET Framework
 * Build and Run a Demonstration Application
 * Troubleshooting
 * REFERENCES



SUMMARY
This step-by-step article shows you the benefits of using the StringBuilder class over traditional concatenation techniques. Strings in the .NET Framework are invariant (that is, the referenced text is read-only after the initial allocation). This provides many performance benefits and poses some challenges to the developer who is accustomed to C/C++ string manipulation techniques.

back to the top

Description of Strings in the .NET Framework
One technique to improve string concatenation over strcat in Visual C/C++ is to allocate a large character array as a buffer and copy string data into the buffer. In the .NET Framework, a string is immutable; it cannot be modified in place. The C# + concatenation operator builds a new string and causes reduced performance when it concatenates large amounts of text.

However, the .NET Framework includes a StringBuilder class that is optimized for string concatenation. It provides the same benefits as using a character array in C/C++, as well as automatically growing the buffer size (if needed) and tracking the length for you. The sample application in this article demonstrates the use of the StringBuilder class and compares the performance to concatenation.

back to the top

Build and Run a Demonstration Application
 Start Visual Studio .NET or Visual Studio 2005 and create a new Visual C# Console application.  The following code uses the += concatenation operator and the StringBuilder class to time 5,000 concatenations of 30 characters each. Add this code to the main procedure. const int sLen=30, Loops=5000; DateTime sTime, eTime; int i; string sSource = new String('X', sLen); string sDest = &quot;&quot;; // // Time string concatenation. // sTime = DateTime.Now; for(i=0;i<Loops;i++) sDest += sSource; eTime = DateTime.Now; Console.WriteLine(&quot;Concatenation took &quot; + (eTime - sTime).TotalSeconds + &quot; seconds.&quot;); // // Time StringBuilder. // sTime = DateTime.Now; System.Text.StringBuilder sb = new System.Text.StringBuilder((int)(sLen * Loops * 1.1)); for(i=0;i Save the application. Press F5 to compile and then run the application. The console windows should display output similar to the following:

Concatenation took 6.208928 seconds.

String Builder took 0 seconds.

Press ENTER to finish...

 Press ENTER to stop running the application and to close the Console window.

back to the top

Troubleshooting

 * If you are in an environment that supports streaming the data, such as in an ASPX Web Form or your application is writing the data to disk, consider avoiding the buffer overhead of concatenation or the StringBuilder, and write the data directly to the stream through the Response.Write method or the appropriate method for the stream in question.
 * Try to reuse the existing StringBuilder class rather than reallocate each time you need one. This limits the growth of the heap and reduces garbage collection. In either case, using the StringBuilder class makes more efficient use of the heap than using the + operator.

back to the top

