.NET 4.0 New Feature - StringBuilder.Clear
Module Module1
Sub Main()
Dim str As New StringBuilder
Dim size As Integer = 100000000
Dim sw As New Stopwatch
'預先編譯降低測試誤差
AppendData(str, size)
ClearData1(str)
ClearData2(str)
ClearData3(str)
'開始測試
AppendData(str, size)
Console.WriteLine("Test Remove...")
sw.Restart()
ClearData1(str)
Console.WriteLine(sw.ElapsedMilliseconds)
AppendData(str, size)
Console.WriteLine("Test Length...")
sw.Restart()
ClearData2(str)
Console.WriteLine(sw.ElapsedMilliseconds)
AppendData(str, size)
Console.WriteLine("Test Clear...")
sw.Restart()
ClearData3(str)
Console.WriteLine(sw.ElapsedMilliseconds)
End Sub
Private Sub AppendData(ByVal str As StringBuilder, ByVal size As Integer)
str.Append(New String("0", size))
End Sub
Private Sub ClearData1(ByVal str As StringBuilder)
str.Remove(0, str.Length)
End Sub
Private Sub ClearData2(ByVal str As StringBuilder)
str.Length = 0
End Sub
Private Sub ClearData3(ByVal str As StringBuilder)
str.Clear()
End Sub
End Module 感覺速度上是沒有太大的差異,挑自己順手的方法寫就好了。 Link StringBuilder.Clear 方法