Brettb.Com
  HOME | ABOUT ME | BIOTECHNOLOGY | ARTICLES | GALLERY | CONTACT
Search: Go
TECHNICAL ARTICLES
 ASP
 ASP.NET
 JavaScript
 Transact SQL
 Other Articles
 Software Reviews

PHOTO GALLERIES
 Canon EOS 300D Samples
 Akihabara Maids!
 More Galleries...

TRAVEL LOG
 2009: China
 2008: Tokyo
 2007: Tokyo
 2006: Hong Kong
 2005: New York City

MORE STUFF
 Search Engine Optimisation
 Build an ASP Search Engine
 My Tropical Fishtank
 Autoglass
 SQL Month Name
 SQL Get Date Today
 SQL Year Month
 Other New Stuff...

POPULAR STUFF
 Regular Expressions
 Index Server & ASP
 JavaScript Ad Rotator

Home > ASP.NET Articles

Improving .NET performance using the StringBuilder Class

Improving the performance of applications can be a complex and time consuming operation. Thankfully, there are a few development techniques that are relatively straightforward to implement and can result in significant application performance benefits.

One weakness of Visual Basic derived languages such as Visual Basic 6.0 and the VBScript scripting language commonly used to write ASP scripts is that string concatenation is very inefficient. This inefficiency can be magnified exponentially if concatenation is performed during a looping operation, such as joining together records from an ADO RecordSet. The inefficiency exists because every time a concatenation is performed a new string object must be created in the memory. The inefficiencies of string concatenation also affect the .NET Framework.

Fortunately, Microsoft has included the solution to the string concatenation problem within the Framework itself. This solution is in the form of the StringBuilder class, which may be found in the System.Text namespace.

If you are experiencing performance problems with code that performs a lot of string concatenation, then this optimization technique is well worth considering. Note that a similar technique can also be used with ASP/VBScript and Visual Basic 6.0. You can either write your own string builder class or use a search engine to find a prewritten algorithm. This article describes how to implement an equivalent to the StringBuilder class in both VBScript and Visual Basic 6.0.

Using the StringBuilder class

The StringBuilder class is a member of the System.Text namespace and can be instantiated using the following code.

Code Listing 1

DimNewStringBuilderAsNewSystem.Text.StringBuilder

Once an instance of the class has been instantiated, text may be added to the class by using the Append method, as shown below.

Code Listing 2

NewStringBuilder.Append("Hello")

Subsequent text may be concatenated to the text already contained in the class by making further uses of the Append method.

Code Listing 3

NewStringBuilder.Append("World")

Finally, once all of the concatenations have been performed, the entire text may be accessed using the ToString() method of the class.

Code Listing 4

NewStringBuilder.ToString()

Note that the StringBuilder class contains a number of other methods and properties that are beyond the scope of this article.

A demonstration of string concatenation performance

The VB.NET sample code below can be used to demonstrate the relative performance of string concatenation using either the traditional method or by making use of the StringBuilder class. The code is currently configured to perform 50,000 iterations of each concatenation technique.

Code Listing 5

PrivateSubPage_Load(ByValsenderAsSystem.Object, ByValeAsSystem.EventArgs)HandlesMyBase.Load
DimOldMethodStartAsNewTimeSpan
DimOldMethodEndAsNewTimeSpan
DimOldStringAsString=""
DimNewMethodStartAsNewTimeSpan
DimNewMethodEndAsNewTimeSpan
DimNewStringAsString=""
DimNewStringBuilderAsNew System.Text.StringBuilder

'Oldmethodofappendingstrings
OldMethodStart=DateTime.Now.TimeOfDay
DimiAsInteger=0
Fori=1To50000
OldString=OldString&"Number" &i.ToString()
Next

OldMethodEnd=DateTime.Now.TimeOfDay

Response.Write("Oldmethodtook")
Response.Write((OldMethodEnd.TotalMilliseconds-
OldMethodStart.TotalMilliseconds).ToString("N2"))
Response.Write("milliseconds.")

'Newmethodofappendingstrings
NewMethodStart=DateTime.Now.TimeOfDay
DimjAsInteger=0
Forj=1To50000
NewStringBuilder.Append("Number" &j.ToString())
Next

NewMethodEnd=DateTime.Now.TimeOfDay

Response.Write("newmethodtook")
Response.Write((NewMethodEnd.TotalMilliseconds-
NewMethodStart.TotalMilliseconds).ToString("N2"))
Response.Write("milliseconds.")
EndSub

As an example, this code was run three times using 10,000, 25,000 and 50,000 iterations. With 10,000 iterations, the traditional string concatenation method took an average of 4831.5 milliseconds. By contrast, the string append method took 10.4 milliseconds. The difference is exponentially greater the more the iterations are performed. With 25,000 iterations, the traditional string concatenation method took an average of 29,239.2 milliseconds and the string append method took only 20.8 milliseconds. At 50,000 iterations, the traditional method took an average of 116,899.4 milliseconds and the string append method took 41.7 milliseconds.

Summary

It is easy to see from these examples that replacing your string concatenation routines with the StringBuilder class may drastically improve your application's performance.

  Site Map | Privacy Policy

All content is 1995 - 2012