Performance difference between String and StringBuilder : StringBuilder « Data Type « VB.Net Tutorial






Imports System.Text
 
public class Test

   public Shared Sub Main
        Const ADD_STRING As String = "1234567890"
        Dim num_trials As Long = 1000
        Dim start_time As DateTime
        Dim stop_time As DateTime
        Dim elapsed_time As TimeSpan
        Dim txt As String
        Dim string_builder As New StringBuilder

        txt = ""
        start_time = Now
        For i As Long = 1 To num_trials
            txt = txt & ADD_STRING
        Next i
        stop_time = Now
        elapsed_time = stop_time.Subtract(start_time)
        Console.WriteLine(elapsed_time.TotalSeconds.ToString("0.000000"))

        txt = ""
        start_time = Now
        For i As Long = 1 To num_trials
            string_builder.Append(ADD_STRING)
        Next i
        txt = string_builder.ToString()
        stop_time = Now
        elapsed_time = stop_time.Subtract(start_time)
        Console.WriteLine(elapsed_time.TotalSeconds.ToString("0.000000"))

   End Sub

End class
0.015625
0.000000








2.40.StringBuilder
2.40.1.Demonstrating StringBuilder class constructors
2.40.2.StringBuilder: Insert string, Append and Replace
2.40.3.Append Char to StringBuilder
2.40.4.Demonstrating StringBuilder Append methods
2.40.5.StringBuilder AppendFormat
2.40.6.StringBuilder.AppendFormat: {0:D} is ${1: #,###.00}
2.40.7.Replace method.
2.40.8.Insert method of StringBuilder
2.40.9.StringBuilder: Length, Capacity, EnsureCapacity
2.40.10.Use StringBuilder Indexer
2.40.11.Demonstrating methods Insert and Remove of the StringBuilder class
2.40.12.Demonstrating method Replace
2.40.13.Use StringBuilder to reverse a string
2.40.14.StringBuilder.ToStrings
2.40.15.Performance difference between String and StringBuilder