Microsoft KB Archive/306823

From BetaArchive Wiki

Article ID: 306823

Article Last Modified on 6/18/2003



APPLIES TO

  • Microsoft JScript .NET
  • Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
  • Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2



This article was previously published under Q306823

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

For a Microsoft Visual C# .NET version of this article, see 306822.

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 top

Description 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 top

Create a Test Application

  1. Create a folder for your application. Name the folder C:\Testfolder.
  2. 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 top

Compile the Application

  1. Start Microsoft Visual Studio .NET.
  2. In Visual Studio .NET, type the following command at a command prompt, and then press ENTER:

    cd C:\testfolder

  3. 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 top

Test the Application

To test the application, type the following command, and then press ENTER:

sbConcat


You may receive the following application output in the console:

Concatenation took 10.65625 seconds.
String Builder took 0 seconds.

back to the top

Troubleshoot

  • 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

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

306600 HOW TO: Use the JScript .NET Command-Line Compiler from Any Directory


For more information about the StringBuilder class, visit the following Microsoft Web sites:

back to the top

Keywords: kbhowtomaster kbperformance kbscript kbstring KB306823