StringBuilder and String Concatenation in VB.NET

String concatenation is one of the commonly used operations among programmers. If you don't handle the string concatenation properly, it may be decrease the performance of an application.
  • 5312

String concatenation is one of the commonly used operations among programmers. If you don't handle the string concatenation properly, it may be decrease the performance of an application.

You can concatenate strings in two ways. First, traditional way of using string and adding the new string to an existing string. In the .NET Framework, this operation is costly. When you add a string to an existing string, the Framework copies both the existing and new  data to the memory, deletes the existing string, and reads data in a new string. This operation can be very time consuming in lengthy string
concatenation operations.

The second and better way to concatenate strings is using the StringBuilder. The StringBuidler class provides the Append method, that inserts a new string to an existing string. 

To prove this theory, we will write two routines and both will repeat the same operations. One is using the string class and second is using the StringBuilder class.
Listing 1 shows the routine that uses string class to concatenate strings.

As you can see from Listing 1, I make a loop and concatenate string using the "+" operator. Before I start the loop, I display the start time and when the loop is done, I display the stop time. 

' Concatenation using string
Console.WriteLine("String routine")
Dim str As String = String.Empty
Dim startTime As DateTime = DateTime.Now
Console.WriteLine(("Start time:" + startTime.ToString()))
Dim i As Integer
For
i = 0 To 9
str += i.ToString()
Next i
Dim stopTime As DateTime = DateTime.Now
Console.WriteLine(("Stop time:" + stopTime.ToString()))

Listing 1.

Now let's see the routine that uses the StringBuidler class. As you can see from Listing 2, we do the exact sample operation but this time we use StringBuilder.Append method to concatenate strings.

' Concatenation using StringBuilder
Console.WriteLine("StringBuilder routine")
Dim builder As New StringBuilder()
startTime = DateTime.Now
Console.WriteLine(("Start time:" + startTime.ToString()))
Dim i As Integer
For
i = 0 To 9
builder.Append(i.ToString())
Next i
stopTime = DateTime.Now
Console.WriteLine(("Stop time:" + stopTime.ToString()))

Now let's run the application. You may not notice the difference for the  loop from 1 to 10 as you can see from the following figure because the difference is in milliseconds.

StringConcat1.jpg

But if you change the loop as following: 

for i=0 to 10000 step=1.

You will notice the difference in seconds as you can see from the following Figure.

StringConcat2.jpg

What about changing loop to the following?

for i=0 to 10000 step=1.

Now you will see a big difference. This time the difference is in minutes as you can see from the following figure.

StringConcat3.jpg

So just be cautious when concatenating strings more than just once. Here is the complete listing of the sample code:

Imports System
Imports System.Text
Namespace StringBuilderSamp
_
Class Class1
Shared Sub Main()
' Concatenation using string
Console.WriteLine("String routine")
Dim str As String = String.Empty
Dim startTime As DateTime = DateTime.Now
Console.WriteLine(("Start time:" + startTime.ToString()))
Dim i As Integer
For
i = 0 To 99999
str += i.ToString()
Next i
Dim stopTime As DateTime = DateTime.Now
Console.WriteLine(("Stop time:" + stopTime.ToString()))
' Concatenation using StringBuilder
Console.WriteLine("StringBuilder routine")
Dim builder As New StringBuilder()
startTime = DateTime.Now
Console.WriteLine(("Start time:" + startTime.ToString()))
Dim i As Integer
For
i = 0 To 99999.
builder.Append(i.ToString())
Next i.
stopTime = DateTime.Now.
Console.WriteLine(("Stop time:" + stopTime.ToString())).
End Sub 'Main.
End Class 'Class1.
End Namespace 'StringBuilderSamp.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.