Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCompare the speeds of Trim$(s) and s.Trim() in VB .NET
DescriptionThis example shows how to compare the speeds of Trim$(s) and s.Trim() in VB .NET
Keywordstrim, Trim$, VB.NET, string
CategoriesStrings, 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!

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated