| Improving the performance of VB.NET applications can be a complex and
time consuming operation. Thankfully, there are a few coding techniques that are easy to
implement and can result in significant performance benefits. This VB.NET tutorial
demonstrates how to improve string handling performance in application code. A weakness
of all Visual Basic derived languages such as Visual Basic 6.0, VBScript and now VB.NET is
the inefficient string concatenation handling. This inefficiency can be magnified hugely
if the string concatenation routine is performed during a looping operation, such as
joining together records from an ADO.NET RecordSet. The inefficiency exists because every
time a concatenation is performed a new string object must be created in the memory.
The solution is to use a custom string concatenation class. In Visual Basic 6.0 or
VBScript this could be achieved through writing such a class. However, Microsoft have
usefully included a string concatenation class within the .NET Framework itself. This
class is the StringBuilder class, which is located in the System.Text namespace.
How to make use of the .NET Framework StringBuilder class
The StringBuilder class is a member of the System.Text
namespace and can be instantiated using the following code.
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.
NewStringBuilder.Append("Hello")
Subsequent text may be concatenated to the text already contained in the class by
making further uses of the Append method.
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.
NewStringBuilder.ToString()
Incidentally, the StringBuilder class contains a number of other methods and
properties that are beyond the scope of this tutorial - check the StringBuilder class
reference in the MSDN documentation for further details.
The StringBuilder class's 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.
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 VB.NET code sample 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 (464 times faster). 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 (1405 times faster). At 50,000 iterations, the
traditional method took an average of 116,899.4 milliseconds and the string append method
took 41.7 milliseconds (which is a massive 2803 times faster).
Summary and Conclusions
These examples demonstrate that replacing basic VB.NET string concatenation routines
with the .NET Framework's StringBuilder class may drastically improve your application's
performance, pleasing developers and end-users alike.
|