|
|
Title | Compare the speeds of Trim$(s) and s.Trim() in VB .NET |
Description | This example shows how to compare the speeds of Trim$(s) and s.Trim() in VB .NET |
Keywords | trim, Trim$, VB.NET, string |
Categories | Strings, Tips and Tricks |
|
|
This example compares the speeds of the Trim$ function and using a string's Trim method.
|
|
start_time = Now
For i As Integer = 1 To num_trials
s2 = Trim$(s1)
Next i
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
lblTrimS.Text = _
elapsed_time.TotalSeconds.ToString("0.000000")
lblTrimS.Refresh()
start_time = Now
For i As Integer = 1 To num_trials
s2 = s1.Trim()
Next i
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
lblSTrim.Text = _
elapsed_time.TotalSeconds.ToString("0.000000")
lblSTrim.Refresh()
|
|
In these tests, the Trim$ function was about 50 percent faster. Note, however, that a string's Trim method has more flexibility because it can take an array of characters to trim.
Michael Freidgeim also notes that Trim removes only spaces while String.Trim() removes all whitespace characters (tab, carriage return, line feed, etc.) by default. Visit his blog!
|
|
|
|
|
|