VB.NET introduced a new way to concatenate strings, StringBuilder class. In VB.NET strings are immutable. This means that once a string is created its value can not be changed. So when your code concatenates strings, the strings themselves pointed by a string variable does not change. Instead a new string object is created and the string variable starts to reference this new string object.
Does this make any difference when concatenating strings in VB.NET code? Not really if you have a simple: MyString = "Hello " & "world!". But things get quite different when your code concatenates strings inside loops or you have otherwise excessive string manipulation in your code.
Concatenate strings with & -operator
Here's a simple procedure that makes concatenation in a loop with & -operator and finally shows elapsed time. Notice that the timing is done in a simple way. The timing in itself is not precise but enough to show the difference between two ways to concatenate strings.
Imports System.Text
''' <summary> ''' Concatenate strings with & -operator ''' </summary> ''' <remarks></remarks> Public Sub ConcatenateString() ' Dim StartTime As DateTime Dim Elapsed As Double Dim ResultString As String Dim TempStr As String Dim i As Integer ResultString = "" TempStr = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit." StartTime = System.DateTime.Now For i = 0 To 9999 ResultString = ResultString & "Line " & i & " " & TempStr & Environment.NewLine Next i Elapsed = System.DateTime.Now.Subtract(StartTime).TotalMilliseconds MessageBox.Show("Elapsed time " & Elapsed.ToString & " ms with & -operator", _ "Elapsed Time", _ MessageBoxButtons.OK, _ MessageBoxIcon.Information) End Sub
and the result is
Concatenate strings with StringBuilder class
Below is the same procedure as above. Now the concatenation of the strings is done with the StringBuilder object.
Imports System.Text
''' <summary> ''' Concatenate strings with StringBuilder ''' </summary> ''' <remarks></remarks> Public Sub ConcatenateStringBuilder() ' Dim StartTime As DateTime Dim Elapsed As Double Dim ResultString As String Dim TempStr As String Dim oStrBuilder As StringBuilder Dim i As Integer ResultString = "" TempStr = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit." oStrBuilder = New StringBuilder() StartTime = System.DateTime.Now For i = 0 To 9999 oStrBuilder.Append("Line ") oStrBuilder.Append(i) oStrBuilder.Append(" ") oStrBuilder.Append(TempStr) oStrBuilder.Append(Environment.NewLine) Next i ' Move the result to ResultString variable ResultString = oStrBuilder.ToString Elapsed = System.DateTime.Now.Subtract(StartTime).TotalMilliseconds MessageBox.Show("Elapsed time " & Elapsed.ToString & " ms with StringBuilder", _ "Elapsed Time", _ MessageBoxButtons.OK, _ MessageBoxIcon.Information) End Sub
and now the result is
Conclusions
In the codes above the difference between methods was 17.5 seconds versus 0.015 seconds. So the conclusion is clear, StringBuilder class gave over 1 000 times faster results than & -operator. When your code concatenates strings inside loops or you have otherwise excessive string manipulation, use StringBuilder class to get most of your application. But as I stated in the beginning of this post, & -operator is still useful. Using StringBuilder class in each and every simple concatenation of the strings would be an overkill.
But there's much more in StringBuilder class than just concatenating strings. Check Microsoft's reference for a complete list of the features StringBuilder class has to offer.
3 comments:
Thanks for such vital source code
Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
Mobile App Developers in India
Thank you for sharing your source code.It will really hep us.
Mobile app development Houston
Post a Comment