Home All Groups Group Topic Archive Search About

Difference between Trim function and Trim member

Author
7 Apr 2005 6:51 AM
Sascha Herpers
Hi,

what is the difference between the trim function and the trim
String-member?
As far as I see it, both return the trimmed string and leave the
original string unaltered.
Is any of the two faster? Is there a general rule/opinion to prefere
members over functions?

Thanks for any hint.

      Sascha

Author
7 Apr 2005 7:17 AM
Stephany Young
The Trim function implemented as a method of the Microsoft.VisualBasic
namespace and acts identically to the VB6 Trim function. I.E., It returns a
string containing a copy of a specified string with no leading or trailing
spaces.

The Trim method is implemented as a method of the System.String class and it
returns a new string equivalent to the specified string after white space
characters are removed from the beginning and end. In addition, this method
has an overload where one can specify which character(s) should be removed.

Calling the Trim function is the same as VB6:
    _s = Trim(_s)
whereas the Trim method operates on a string instance:
    _s = _s.Trim()

Because white space comprises characters in addition to the space character,
if you want to remove only spaces then you must call the the Trim function
or the overloaded Trim method with a parameter of " "c.

I have run tests that show that using the Trim method gives a marginal
performance saving, and I stress the word marginal. Any other evidence I
have heard or read on the performance of the Trim function over the Trim
method or vice-versa is purely anecdotal.


Show quoteHide quote
"Sascha Herpers" <herp***@wiso.uni-koeln.de> wrote in message
news:a55dad94.0504062251.32351185@posting.google.com...
> Hi,
>
> what is the difference between the trim function and the trim
> String-member?
> As far as I see it, both return the trimmed string and leave the
> original string unaltered.
> Is any of the two faster? Is there a general rule/opinion to prefere
> members over functions?
>
> Thanks for any hint.
>
>      Sascha
Author
7 Apr 2005 10:04 AM
Herfried K. Wagner [MVP]
"Stephany Young" <noone@localhost> schrieb:
> I have run tests that show that using the Trim method gives a marginal
> performance saving, and I stress the word marginal. Any other evidence I
> have heard or read on the performance of the Trim function over the Trim
> method or vice-versa is purely anecdotal.

I took a look at the implementation of 'String.Trim' and 'Strings.Trim'
using Reflector
(<URL:http://www.aisto.com/roeder/dotnet/Download.aspx?File=Reflector.zip>).
'Strings.Trim' performs some checks first and then calls 'String.Trim'.  My
conclusion is that it's up to personal preference whether to use
'String.Trim' or 'Strings.Trim'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 Apr 2005 11:25 AM
Stephany Young
Agreed.

That 'Reflector' shows some interesting things.

Looking at Strings.Trim, it is understandable that String.Trim is marginally
faster.

By the same token, looking at Strings.Mid v String.Substring, one would
expect String.Substring to be marginally faster, however, observation shows
that Strings.Mid is actually the faster method.

Curiouser and curioser ...


Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:OucHdk1OFHA.3048@TK2MSFTNGP10.phx.gbl...
> "Stephany Young" <noone@localhost> schrieb:
>> I have run tests that show that using the Trim method gives a marginal
>> performance saving, and I stress the word marginal. Any other evidence I
>> have heard or read on the performance of the Trim function over the Trim
>> method or vice-versa is purely anecdotal.
>
> I took a look at the implementation of 'String.Trim' and 'Strings.Trim'
> using Reflector
> (<URL:http://www.aisto.com/roeder/dotnet/Download.aspx?File=Reflector.zip>).
> 'Strings.Trim' performs some checks first and then calls 'String.Trim'.
> My conclusion is that it's up to personal preference whether to use
> 'String.Trim' or 'Strings.Trim'.
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
Author
7 Apr 2005 1:55 PM
Chris Dunaway
I'm confused.  While I don't doubt you because I have not run any
tests, but why would Strings.Mid be any faster?  It just calls
String.Substring anyway.  What am I missing?
Author
7 Apr 2005 5:43 PM
Stephany Young
My question, precisely.


Show quoteHide quote
"Chris Dunaway" <dunaw***@gmail.com> wrote in message
news:1112882114.279974.195990@l41g2000cwc.googlegroups.com...
> I'm confused.  While I don't doubt you because I have not run any
> tests, but why would Strings.Mid be any faster?  It just calls
> String.Substring anyway.  What am I missing?
>
Author
7 Apr 2005 5:58 PM
Herfried K. Wagner [MVP]
"Stephany Young" <noone@localhost> schrieb:
> My question, precisely.
>
>> I'm confused.  While I don't doubt you because I have not run any
>> tests, but why would Strings.Mid be any faster?  It just calls
>> String.Substring anyway.  What am I missing?

That's hard to decide as long as we don't know anything about the internal
implementation of 'Substring'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
7 Apr 2005 7:57 PM
Chris Dunaway
True, but in the implementation for Strings.Mid, there doesn't seem to
be anything that would make its call to String.Substring any faster
than a direct call to String.Substring.  I don't see how the internal
implementation of substring is relevant.

When I look at the code for String.Substring, I see this:

Public Function Substring(ByVal startIndex As Integer, ByVal length As
Integer) As String
      Dim num1 As Integer = Me.Length
      If (startIndex < 0) Then
            Throw New ArgumentOutOfRangeException(...)
      End If
      If (length < 0) Then
            Throw New ArgumentOutOfRangeException(...)
      End If
      If (startIndex > (num1 - length)) Then
            Throw New ArgumentOutOfRangeException(...)
      End If
      Dim text1 As String = String.FastAllocateString(length)
      String.FillSubstring(text1, 0, Me, startIndex, length)
      Return text1
End Function

And when I look at the code for Strings.Mid I see this:

Public Shared Function Mid(ByVal str As String, ByVal Start As Integer,
ByVal Length As Integer) As String
      If (Start <= 0) Then
            Throw New ArgumentException(...)
      End If
      If (Length < 0) Then
            Throw New ArgumentException(...)
      End If
      If ((Length = 0) OrElse (str Is Nothing)) Then
            Return ""
      End If
      Dim num1 As Integer = str.Length
      If (Start > num1) Then
            Return ""
      End If
      If ((Start + Length) > num1) Then
            Return str.Substring((Start - 1))
      End If
      Return str.Substring((Start - 1), Length)
End Function

Except for the few additional checks that Mid performs, it is just a
call to Substring.  what would make Mid run faster?  To me, it should
run slower because of the extra checks.  I am still confused.
Author
7 Apr 2005 5:53 PM
Herfried K. Wagner [MVP]
Stephany,

"Stephany Young" <noone@localhost> schrieb:
> Looking at Strings.Trim, it is understandable that String.Trim is
> marginally faster.

I didn't yet take a look at the implementation of 'String.Trim', but I am
curious why the VB.NET team decided to check the value passed to the
function before calling the string's 'Trim' method.  It depends on the
implementation of the 'TrimHelper' method if this makes sense or not, and if
it increases the performance or not.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>