|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Difference between Trim function and Trim memberHi,
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 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 "Stephany Young" <noone@localhost> schrieb: I took a look at the implementation of 'String.Trim' and 'Strings.Trim' > 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. 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/> 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/> 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? 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? > "Stephany Young" <noone@localhost> schrieb: That's hard to decide as long as we don't know anything about the internal > 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? implementation of 'Substring'. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> 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. Stephany,
"Stephany Young" <noone@localhost> schrieb: I didn't yet take a look at the implementation of 'String.Trim', but I am > Looking at Strings.Trim, it is understandable that String.Trim is > marginally faster. 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/>
New To Imaging
DateTimePicker Bug? How to create a Serialnumber for App programmatically? How to get form design view back? sending string thru TCP/IP Can I pass value from VB form to VBS? Using MSCOMM32.ocx with VB.net listboxes to select/deselect multiple items SharpZipLib & Blocked File Help Please |
|||||||||||||||||||||||