The Book of Visual Studio .NET - A Visual Basic .NET Crash Course
Boxing occurs when a value type is converted to a reference type and recreated on the heap. Boxing should be used sparingly as the ability to move values from the stack to the heap is performance intensive.
The most common occurrence of boxing is when a value type variable is passed to a procedure that accepts the System.Object data type. System.Object is the equivalent of the classic Visual Basic variant data type.
The ReDim statement, available in classic Visual Basic, is still available in Visual Basic .NET. Classic V isual Basic no t only allowed developers to rediminish an array, but also initialize the array. Visual Basic .NET allows the use of ReDim to rediminish an array but not to initialize an array.
The StringBuilder class is an impressive class optimizing string manipulation. You'll better understand its advantages once you understand how string manipulation has historically worked.
Classic Visual Basic hid the actual implementation code supporting functions available in the Visual Basic library, and string manipulation was no exception. One common string function is the concatenation of two strings. Unfortunately, Visual Basic doesn't simply add the two strings together; instead, the windows system determines the space required for the new string, allocates memory, and places the new concatenated value into the newly allocated memory. The StringBuilder class is implemented as an array of characters. This allows it to implement methods to manipulate what appears to be a string without the overhead incurred by an actual string. The Insert method of the StringBuilder class is used to add to the character array in a way that is much more efficient than classic string manipulation, increasing performance of many common programming scenarios. (You'll find the StringBuilder class in the System.Text namespace.)
Using the StringBuilder
This example will show you how to use the StringBuilder class and will compare its performance against the performance of classic Visual Basic string concatenation. To begin, follow these steps:
- Create a windows project and build a window that looks the same as Figure 7-1, using the parameters in Table 7-1.
- Add the following code segment to the click event of the btnString button.
Dim dateStart As Date Dim strString As String Dim i As Integer dateStart = DateAndTime.TimeOfDay For i = 1 To 15000 strString = strString & "string value " Next i lblStringDisplay.Text = DateAndTime.DateDiff(DateInterval.Second, _ dateStart, DateAndTime.TimeOfDay) & " Seconds"
- Add the following code segment to the click event of the btnStringBuilder button.
Dim dateStart As Date Dim objString As New System.Text.StringBuilder() Dim i As Integer dateStart = DateAndTime.TimeOfDay For i = 1 To 15000 objString = objString.Append("string value ") Next i lblStringBuilderDisplay.Text = DateAndTime.DateDiff( _ DateInterval.Second, dateStart, _ DateAndTime.TimeOfDay) & " Seconds"
- Now run the example and press each button. You will see a significant difference between the performances of the two methods of string concatenation.
Click for a full image.
Figure 7-1: Using the StringBuilder class.
Table 7-1: Parameters for the StringBuilder Class
Control Property Value
Button Name btnString|
Text strString = strString & "string value"
Button Name BtnStringBuilder
Text objStringBuilder = objStringBuilder.Append("string value")
Label Name lblStringDisplay
Label Name lblStringBuildingDisplay
Label Text The first button concatinates strings the classic VB way while the second button used the StringBuilder class. Each will loop through each concatination 15000 times.
Previously, strings were built by simply adding one onto the end of another. This only seems to be what is happening. What is actually occurring is something different. When adding one string to another, you begin with the original string in memory, then a new string is allocated in memory for the string being added. Next, a new string representing the new concatenated string is created and the new string placed into it, and finally, the original string and the added string are de-allocated, leaving only the newly concatenated string in memory.
As you might imagine, this is a very inefficient process for simply adding two string values together. The StringBuilder class is a collection of characters. The StringBuilder character collection can allow values to be added and removed without the need to re-allocate and de-allocate memory blocks. As you will see, the performance difference is significant.
Page 2 of 5