SUMMARY
This step-by-step article describes how to improve
performance of string concatenation by using the
StringBuilder class instead of conventional concatenation
techniques.
back to the
topDescription of Strings in the .NET Framework
When strings are being concatenated, the
+ concatenation operator builds a new string each time. A large
amount of string concatenation that uses the
+ operator may affect performance. However, the .NET Framework
includes a
StringBuilder class that is optimized for string concatenation and is much
faster and more efficient than the
+ operator while concatenating multiple strings.
The
StringBuilder class can allocate more memory as required to store characters
when the value of an instance is enlarged. The capacity is adjusted
automatically. When an instance of
StringBuilder is initialized, a default capacity is used if the capacity is not
specified. This improves performance in string concatenation.
The
sample application in this article demonstrates how to use the
StringBuilder class, and compares the performance to conventional
concatenation.
back to the
topCreate a Test Application
- Create a folder for your application. Name the folder
C:\Testfolder.
- In a text editor (such as Notepad), paste the following
Jscript .NET code, and then save the file as C:\Testfolder\
sbConcat.js:
import System;
const sLen:int = 30, Loops:int = 5000;
var sTime:DateTime, eTime:DateTime;
var i:int;
var sSource:String = new System.String('X', sLen);
var sDest:String = "";
//
// Time string concatenation
//
sTime = DateTime.Now;
for(i=0;i<Loops;i++) sDest += sSource;
eTime = DateTime.Now;
Console.WriteLine("Concatenation took " + (eTime - sTime).TotalSeconds + " seconds.");
//
// Time StringBuilder
//
sTime = DateTime.Now;
var sb:System.Text.StringBuilder = new System.Text.StringBuilder((int)(sLen * Loops * 1.1));
for(i=0;i<Loops;i++) sb.Append(sSource);
sDest = sb.ToString();
eTime = DateTime.Now;
Console.WriteLine("String Builder took " + (eTime - sTime).TotalSeconds + " seconds.");
back to the topCompile the Application
- Start Microsoft Visual Studio .NET.
- In Visual Studio .NET, type the following command at a
command prompt, and then press ENTER:
- To compile the application, type the following command, and
then press ENTER:
jsc /target:exe sbConcat.js
Note The sbContact.exe file is created in the C:\Testfolder
folder.
back to the topTest the Application
To test the application, type the following command, and then
press ENTER:
You may receive the following application output in the console:
Concatenation took 10.65625 seconds.
String Builder took 0 seconds.
back to the
topTroubleshoot
- If your computer is in an environment that supports
streaming the data (such as in an ASPX Web Form), or your application is
writing the data to a hard disk, you can avoid the buffer overhead of
concatenation or the StringBuilder class by writing the data directly to the stream. To do this, you
can use the Response.Write method, or use any method that is appropriate for the stream that
you are writing to.
- Try to reuse the existing StringBuilder, instead of reallocating each time that you require one. If you
reuse the StringBuilder, you might avoid growing the heap and triggering unnecessary
garbage collections. In either case, if you use the StringBuilder, you use the heap more efficiently than if you use
the + concatenation operator.
back to the
top