AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=930&pId=-1
Boosting the Performance of an ASP.NET Application Using StringBuilder Class
page
by Brett Burridge
Feedback
Average Rating: 
Views (Total / Last 10 Days): 29512/ 58

Introduction

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.

The Problem

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.

The Solution

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.

Listing 1

Dim NewStringBuilder As New System.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.

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.

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.

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.

Listing 5

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim OldMethodStart As New TimeSpan
  Dim OldMethodEnd As New TimeSpan
  Dim OldString As String = ""
  Dim NewMethodStart As New TimeSpan
  Dim NewMethodEnd As New TimeSpan
  Dim NewString As String = ""
  Dim NewStringBuilder As New System.Text.StringBuilder
 
  'Old method of appending strings
  OldMethodStart = DateTime.Now.TimeOfDay
  Dim i As Integer = 0
  For i = 1 To 50000
    OldString = OldString & "Number " & i.ToString()
  Next
 
  OldMethodEnd = DateTime.Now.TimeOfDay
 
  Response.Write("Old method took ")
  Response.Write((OldMethodEnd.TotalMilliseconds -
  OldMethodStart.TotalMilliseconds).ToString("N2"))
  Response.Write(" milliseconds.")
 
  'New method of appending strings
  NewMethodStart = DateTime.Now.TimeOfDay
  Dim j As Integer = 0
  For j = 1 To 50000
    NewStringBuilder.Append("Number " & j.ToString())
  Next
 
  NewMethodEnd = DateTime.Now.TimeOfDay
 
  Response.Write("new method took ")
  Response.Write((NewMethodEnd.TotalMilliseconds -
  NewMethodStart.TotalMilliseconds).ToString("N2"))
  Response.Write(" milliseconds.")
End Sub

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.

References

[Editor]Here are some additional references on this topic, which may help demonstrate when one should use a StringBuilder.

Are StringBuilders always faster than concatenation? - Bertrand LeRoy

More on string concatenation performance - Bertrand LeRoy

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.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 6:52:01 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search