<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx</link><description>yesterday I posted a bit of code that was checkign to see if a string was whitespace only. Let's assume the assumption I made was correct, that checkign the middle of the string then spreading out in each direction is the best way of doing that. So the</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#52283</link><pubDate>Sun, 12 Jun 2005 19:44:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:52283</guid><dc:creator>bill</dc:creator><description>Hey Jacob,&lt;br&gt;&lt;br&gt;At least on developer's machines, &amp;quot;text&amp;quot; is more likely to have leading whitespace.  As office moves to XML this is going to spread. For use input, leading whitespace is rere (usually only a sinlge space), but trailing whitespace can be common as it is visually not as obvious.  Even then the usual I see is people cut and pasting and getitng on extra trailing whitespace.&lt;br&gt;&lt;br&gt;So i get what you mean about it any character can pobbily be whitespace or not, but I think there is a definetly a probability that the start is less likely than the end for non whitespace.  It's like the alphabet.. although any letter could be any legal char, we usually find a higher percentage of a and e, s and t compared to q or z etc.  So if you had to test for each different letter, you'd be better to test for say s and t earlier on, than alphabetically. &lt;a title="winking smiley" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/wink.gif" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;Re: short circuited versus non, the simple rule I use is if the second expression is not dependant on the first to be legal (eg testing for null first) then bitwise operators can be more performant than branching ones.  The toehr factors are the cost of evaluating the second expression.  But if you expect that to be evaluated the majority of the time anyway then that cost is the same for both.&lt;br&gt;&lt;br&gt;&lt;br&gt;As ot the CAPTCHA, yeh don't even try first time.  Apparently it has a rediculous short time out.  (sorry, but not my doing)&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=52283" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51813</link><pubDate>Sat, 11 Jun 2005 05:50:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51813</guid><dc:creator>bill</dc:creator><description>Yup, my fault on the zero length string.  I saw that it could be taken out of yours, so I left it out of mine. &lt;a title="winking smiley" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/wink.gif" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;re: Stats&lt;br&gt;Well, what you're talking about is the context of that which you are parsing.  Parsing code/html/xml makes sense on that front to start in the middle and work your way out to the ends.  Now, assume that you have some generic text file, you have no knowledge whatsoever of its contents.  You randomly take a line from that file and you randomly take a contiguous sequence of characters from that line.  Any character in that sequence is as likely to be (non)whitespace as any other.  That's the point I was trying to make.&lt;br&gt;&lt;br&gt;&lt;br&gt;Oh, and that's an interesting note on the short-circuit.  I'll have to keep that in mind.  I have reams of code that looks like:&lt;br&gt;&lt;br&gt;Do Until blnFound OrElse i = Count&lt;br&gt;..&lt;br&gt;Loop&lt;br&gt;&lt;br&gt;&lt;br&gt;What's up with your human proof, btw?  It never works the first time for me.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51813" width="1" height="1"&gt;</description></item><item><title>Re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51531</link><pubDate>Fri, 10 Jun 2005 16:58:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51531</guid><dc:creator>bill</dc:creator><description>oh btw: did you notice I used And, not AndAlso.  Given that during the loop you do have to check both (only on exit of the loop can you short circuit), And is actually more efficient than the conditional branching AndAlso &lt;a title="winking smiley" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/wink.gif" border="0"&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51531" width="1" height="1"&gt;</description></item><item><title>Re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51528</link><pubDate>Fri, 10 Jun 2005 16:54:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51528</guid><dc:creator>bill</dc:creator><description>Hey Jacob,&lt;br&gt;&lt;br&gt;On the code, you need to test for a zero length string, or set up your loop such that it isn't entered in that case.  Looking at mine, I could drop the explicit test for a zero length string and it should work okay, which would be about the same in code.  I would do an early exit rather than nest the hwile loop... I just prefer them.... I doubt there is any performance difference between the two on that count.&lt;br&gt;&lt;br&gt;&lt;br&gt;The statistical analysis of strings, I think given the predonimance of &amp;quot;code&amp;quot;, html and xml, leading whitespace is very common.  Given the parsing on a line by line basis is also common, then carriage returns and line feeds can be expected to be very common at the end of a string.  Ideally we want to find a non whitespace character as soon as possible, so looking at the ends is actually the worst place to start.  My guess is, that two or three characters in from the end is the most likely place, BUT considering we have to test all the characters if we find the string is purely whitespace, then startign from other than the ends adds extra complexity.&lt;br&gt;&lt;br&gt;anyway, if it's raining on Sunday and I get bored on Sunday, what I'll do is recursively parse all plain text files in( my documents or something like that) and cache all the strings on a line by line basis, and then test some code with them &lt;a title="" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/smiley.gif" border="0" alt="smile"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51528" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51441</link><pubDate>Fri, 10 Jun 2005 11:45:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51441</guid><dc:creator>bill</dc:creator><description>Hey Bill, I haven't done any testing with this, but in my head this is a little more efficient:&lt;br&gt;&lt;br&gt;Public Function IsWhitespace(ByVal stringToCheck As String) As Boolean &lt;br&gt;IsWhitespace = True &lt;br&gt;Dim i As Int32 &lt;br&gt;&lt;br&gt;If Not stringToCheck is Nothing Then&lt;br&gt;&lt;br&gt;i = stringToCheck.Length -1&lt;br&gt;Do&lt;br&gt;&lt;br&gt;IsWhitespace = Char.IsWhiteSpace(stringToCheck.Chars(i)) &lt;br&gt;i-=1&lt;br&gt;&lt;br&gt;Loop Until Not IsWhitespace OrElse i &amp;lt; 0&lt;br&gt;&lt;br&gt;End If&lt;br&gt;&lt;br&gt;Return IsWhitespace &lt;br&gt;End Function &lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51441" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51439</link><pubDate>Fri, 10 Jun 2005 11:37:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51439</guid><dc:creator>bill</dc:creator><description>With respect to the statistical analysis as to where one should start the inquiry, if you're dealing with a random string, then the odds are highly in your favor that any character you start with will be non-whitespace.  Though, no character has any more likelihood than any other.&lt;br&gt;&lt;br&gt;However, if you have some context about the string and its contents (i.e. it's not random) then some analysis could be performed to increase the likelihood.  Though, I'd be willing to bet that the first or last character would be the most fruitful.&lt;br&gt;&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51439" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51418</link><pubDate>Fri, 10 Jun 2005 06:03:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51418</guid><dc:creator>bill</dc:creator><description>Hey Geoff,&lt;br&gt;&lt;br&gt;uhm... speechless.... totally ... &lt;a title="winking smiley" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/wink.gif" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&amp;lt;slap&amp;gt;&amp;lt;slap&amp;gt;  Ah, no I'm better....  yes, you have convinced me, GoTo is evil.  give it an inch and someone will take it for a mile &amp;lt;bg&amp;gt;&lt;br&gt;&lt;br&gt;i'm going for the easier to maintain code.  I know the following is different code, but simple is good, you've convinced me.  (well unless you tell me the perf suxs that is) &lt;a title="winking smiley" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/wink.gif" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;   Public Function IsWhitespace(ByVal stringToCheck As String) As Boolean&lt;br&gt;      IsWhitespace = True&lt;br&gt;      Dim i As Int32&lt;br&gt;&lt;br&gt;      If stringToCheck Is Nothing OrElse stringToCheck.Length = 0 Then&lt;br&gt;         Return True&lt;br&gt;      End If&lt;br&gt;&lt;br&gt;      i = stringToCheck.Length&lt;br&gt;      While IsWhitespace = True And i &amp;gt; 0&lt;br&gt;         i -= 1&lt;br&gt;         IsWhitespace = Char.IsWhiteSpace(stringToCheck.Chars(i))&lt;br&gt;      End While&lt;br&gt;&lt;br&gt;      Return IsWhitespace&lt;br&gt;&lt;br&gt;   End Function&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51418" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51414</link><pubDate>Fri, 10 Jun 2005 05:48:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51414</guid><dc:creator>bill</dc:creator><description>hey Jacob,&lt;br&gt;&lt;br&gt;Actually that is probalby very efficient.  I think I might switch to that not only becuase of that, but also it adds some consistency.  Although one has to ask why Vb's Trim, and String.Trim don't use that ??  I wonder if they did some benchmarks on calling CharacterInfo.IsWhitespace(char) or not ?  Then again maybe it's just because  String.Trim is designed to do other things. I think that may be it.  I haven't tested, but I think the CharacterInfo.Iswhitespace(char) is probalby the better choice... well it's my bet at this moment anyway &lt;a title="winking smiley" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/wink.gif" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51414" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51363</link><pubDate>Fri, 10 Jun 2005 03:30:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51363</guid><dc:creator>bill</dc:creator><description>Speechless huh? &lt;br&gt;&lt;br&gt;So tell me...NOW are gotos evil? *grin*  I think all it really proves is that you have to be willing to wear the penalties of anything other than walking the entire string from one end to the other. Statisitical analysis (which is what i was trying to emulate while not really doing it) might allow us to find in advance more _likely_ locations of non-whitespace chars, but it is it really worth the penalty of either slower or harder to maintain code?&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51363" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51254</link><pubDate>Thu, 09 Jun 2005 12:44:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51254</guid><dc:creator>bill</dc:creator><description>Char.IsWhitespace(myChar) ...&lt;br&gt;&lt;br&gt;This uses a Select Case statement.  Returns true if the character is &lt;br&gt;ChrW(9)&lt;br&gt;ChrW(10)&lt;br&gt;ChrW(11)&lt;br&gt;ChrW(12)&lt;br&gt;ChrW(13)&lt;br&gt;&amp;quot; &amp;quot;&lt;br&gt;&lt;br&gt;Or if the unicode category is 11,12,13 ..&lt;br&gt;&lt;br&gt;Probably not the most efficient, but it's in there.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51254" width="1" height="1"&gt;</description></item><item><title>Re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51154</link><pubDate>Thu, 09 Jun 2005 04:48:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51154</guid><dc:creator>bill</dc:creator><description>Geoff .... uhm... I'm speechless &lt;a title="winking smiley" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/wink.gif" border="0"&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51154" width="1" height="1"&gt;</description></item><item><title>String Performance Part 2</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#51178</link><pubDate>Wed, 08 Jun 2005 12:04:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:51178</guid><dc:creator>TrackBack</dc:creator><description>I thought I'd talk a little bit more about a post from a few days ago, String Performance Over Different...&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=51178" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#50930</link><pubDate>Wed, 08 Jun 2005 02:58:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:50930</guid><dc:creator>bill</dc:creator><description>Here's an interesting one for you.  based on a few sample strings of random gumpf, this one seemd to deal better with longer strings...&lt;br&gt;&lt;br&gt;    Protected Overrides Function IsWhitespaceOnly(ByVal stringToCheck As String, ByVal ParamArray whitespaceChars() As Char) As Boolean&lt;br&gt;        Dim length As Int32 = stringToCheck.Length&lt;br&gt;        Dim middle As Int32 = length \ 2&lt;br&gt;        Dim quarter As Int32 = middle \ 2&lt;br&gt;        Dim first As Int32 = middle&lt;br&gt;        Dim second As Int32 = middle + 1&lt;br&gt;&lt;br&gt;        Dim chr As Char&lt;br&gt;&lt;br&gt;        If whitespaceChars Is Nothing OrElse whitespaceChars.Length = 0 Then&lt;br&gt;            whitespaceChars = New Char() {&amp;quot; &amp;quot;c, ChrW(&amp;amp;H3000)}&lt;br&gt;        End If&lt;br&gt;&lt;br&gt;        Dim whitespaceUbound As Int32 = whitespaceChars.Length - 1&lt;br&gt;&lt;br&gt;        'check first char&lt;br&gt;        chr = stringToCheck.Chars(0)&lt;br&gt;        For j As Int32 = 0 To whitespaceUbound&lt;br&gt;            If chr = whitespaceChars(j) Then&lt;br&gt;                GoTo firstpart&lt;br&gt;            End If&lt;br&gt;        Next&lt;br&gt;        Return False&lt;br&gt;firstpart:&lt;br&gt;        If length = 1 Then Return True&lt;br&gt;&lt;br&gt;        'check last char&lt;br&gt;        chr = stringToCheck.Chars(length - 1)&lt;br&gt;        For j As Int32 = 0 To whitespaceUbound&lt;br&gt;            If chr = whitespaceChars(j) Then&lt;br&gt;                GoTo secondpart&lt;br&gt;            End If&lt;br&gt;        Next&lt;br&gt;        Return False&lt;br&gt;secondpart:&lt;br&gt;        If length = 2 Then Return True&lt;br&gt;&lt;br&gt;        'check middle char&lt;br&gt;        chr = stringToCheck.Chars(middle)&lt;br&gt;        For j As Int32 = 0 To whitespaceUbound&lt;br&gt;            If chr = whitespaceChars(j) Then&lt;br&gt;                GoTo thirdpart&lt;br&gt;            End If&lt;br&gt;        Next&lt;br&gt;        Return False&lt;br&gt;thirdpart:&lt;br&gt;        If length = 3 Then Return True&lt;br&gt;&lt;br&gt;        'check first quarter char&lt;br&gt;        chr = stringToCheck.Chars(quarter)&lt;br&gt;        For j As Int32 = 0 To whitespaceUbound&lt;br&gt;            If chr = whitespaceChars(j) Then&lt;br&gt;                GoTo sixthpart&lt;br&gt;            End If&lt;br&gt;        Next&lt;br&gt;        Return False&lt;br&gt;sixthpart:&lt;br&gt;&lt;br&gt;        'check third quarter char&lt;br&gt;        chr = stringToCheck.Chars(middle + quarter)&lt;br&gt;        For j As Int32 = 0 To whitespaceUbound&lt;br&gt;            If chr = whitespaceChars(j) Then&lt;br&gt;                GoTo seventhpart&lt;br&gt;            End If&lt;br&gt;        Next&lt;br&gt;        Return False&lt;br&gt;seventhpart:&lt;br&gt;&lt;br&gt;        'check first quarter range backwards&lt;br&gt;        For i As Int32 = quarter - 1 To 1 Step -1&lt;br&gt;            chr = stringToCheck.Chars(i)&lt;br&gt;            For j As Int32 = 0 To whitespaceUbound&lt;br&gt;                If chr = whitespaceChars(j) Then&lt;br&gt;                    GoTo eightpart&lt;br&gt;                End If&lt;br&gt;            Next&lt;br&gt;            Return False&lt;br&gt;eightpart:&lt;br&gt;        Next&lt;br&gt;&lt;br&gt;        'check last quarter range forwards&lt;br&gt;        For i As Int32 = middle + quarter + 1 To length - 2&lt;br&gt;            chr = stringToCheck.Chars(i)&lt;br&gt;            For j As Int32 = 0 To whitespaceUbound&lt;br&gt;                If chr = whitespaceChars(j) Then&lt;br&gt;                    GoTo ninepart&lt;br&gt;                End If&lt;br&gt;            Next&lt;br&gt;            Return False&lt;br&gt;ninepart:&lt;br&gt;        Next&lt;br&gt;&lt;br&gt;        'check second quarter range forwards&lt;br&gt;        For i As Int32 = quarter + 1 To middle - 1&lt;br&gt;            chr = stringToCheck.Chars(i)&lt;br&gt;            For j As Int32 = 0 To whitespaceUbound&lt;br&gt;                If chr = whitespaceChars(j) Then&lt;br&gt;                    GoTo fourthpart&lt;br&gt;                End If&lt;br&gt;            Next&lt;br&gt;            Return False&lt;br&gt;fourthpart:&lt;br&gt;        Next&lt;br&gt;&lt;br&gt;        'check third quarter range backwards&lt;br&gt;        For i As Int32 = middle + quarter - 1 To middle + 1 Step -1&lt;br&gt;            chr = stringToCheck.Chars(i)&lt;br&gt;            For j As Int32 = 0 To whitespaceUbound&lt;br&gt;                If chr = whitespaceChars(j) Then&lt;br&gt;                    GoTo fifthpart&lt;br&gt;                End If&lt;br&gt;            Next&lt;br&gt;            Return False&lt;br&gt;fifthpart:&lt;br&gt;        Next&lt;br&gt;&lt;br&gt;        Return True&lt;br&gt;&lt;br&gt;    End Function&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=50930" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#50918</link><pubDate>Wed, 08 Jun 2005 01:21:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:50918</guid><dc:creator>bill</dc:creator><description>binary search perhaps?&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=50918" width="1" height="1"&gt;</description></item><item><title>Re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#50894</link><pubDate>Tue, 07 Jun 2005 21:11:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:50894</guid><dc:creator>bill</dc:creator><description>Hey Geoff,&lt;br&gt;&lt;br&gt;I think you're wrong about me being right. &lt;a title="winking smiley" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/wink.gif" border="0"&gt;&lt;/a&gt;  It's probably not a bad assumption.  I think when I look at strings, there's a reasonable probability they will start with whitespace.  Chances of them having more than one consequtive whitespace char in the middle is low, but only if the ratio of test to whitespace is generally large.  One example that comes to mind is say parsing an indented XML file.  LEt's assume they use &amp;quot;space&amp;quot; or similar ,not tabs.  There the likelihood of the ratio of whitespace to non-whitespace on any given line is likely to be &amp;gt; 1:1,( depending on the depth of the object model and the type of xml output used) in many cases.  So for those lines, the ideal spot to start would be near the end.  Definetly not at the start, not so the middle, and maybe not the end.  We could optimise this better is we knew the whitespace char list was optimised accordingly.  That is if it checks for cr's and lf' early in the list, then it's only going to be one or two comparisons for the ending chars, rather than the 20 ro so comparisons it can take ot declare a character as not being whitespace.  Obviously the relative efficeincy depends a lot on the whitespace shar list though, whether cr and lf are considered to be whitespace, whether the whitespace lsit is large etc.  But If I take the two standard lsits, VB's 2 char one, and String's 21 char one, then I think searching from the end in will genrally be the most efficient, as we can reduce the complexity of your code, and with the VB char set, as cr or lf is considered non whitespace, and with the string char set the cr and lf are pretty early on in the list, so the cost of those likely couple of comparisons shoudl be trivial compared to the cost of settign up to say work from the middle out.&lt;br&gt;&lt;br&gt;Okay, maybe, maybe not... it's a theory anyway &lt;a title="winking smiley" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/wink.gif" border="0"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=50894" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#50892</link><pubDate>Tue, 07 Jun 2005 20:45:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:50892</guid><dc:creator>bill</dc:creator><description>Bill, no, i think your right in starting from the middle.&lt;br&gt;&lt;br&gt;Remember, the way the mothod is structured is to find one single non-whitespace char ASAP, and bail out immediately if it does. If whitespace mostly appears on the left or right, it doesn't matter. most of the time (and we can only talk probabilities here) a non-whitespace char is highly likely to be somewhere near the middle of the string, or not too far away from there.&lt;br&gt;&lt;br&gt;As for tests, yeah, I have been. I'll run through Matthew code when I get a chance.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=50892" width="1" height="1"&gt;</description></item><item><title>Re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#50889</link><pubDate>Tue, 07 Jun 2005 18:19:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:50889</guid><dc:creator>bill</dc:creator><description>I can't right now, but I'll run some tests later tonight and post back here.  It sounds like Geoff may have run some tests... I actually haven't run any yet.&lt;br&gt;&lt;br&gt;BTW: I actually think my base assumption of starting searching in the middle is probably not a good assumption.  I tend to find strings generally have leading whitespace rather than trailing whitespace, but it would depend on usage. Take for exampel reading from a text file line by line, if the lines actually have the carriage returns and or line feeds on the and of them, and they are considered whitespace, the optimal may be searching from &amp;quot;near&amp;quot; the end but not at the end.  &lt;a title="winking smiley" href="#" &gt;&lt;img src="http://billmccarthy.mvps.org/smileys/bw/wink.gif" border="0"&gt;&lt;/a&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=50889" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#50887</link><pubDate>Tue, 07 Jun 2005 17:44:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:50887</guid><dc:creator>bill</dc:creator><description>The ubound recalculated bothered me too -- I almost passed it in as an extra parameter.  But I thought their might be some use for the IsWhitespaceCharacter as a standalone function, so I didn't.   If you knew it was internal only, then I'd add it as a parameter at the small cost of increased coupling between the two.&lt;br&gt;&lt;br&gt;The call stack usage doesn't bother me too much -- mainly because I think there is a decent chance it would get inlined.  I'd love if someone who has a VB.NET compiler could do a quick perf analysis of the two.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=50887" width="1" height="1"&gt;</description></item><item><title>Re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#50885</link><pubDate>Tue, 07 Jun 2005 17:28:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:50885</guid><dc:creator>bill</dc:creator><description>Hey Matthew,&lt;br&gt;&lt;br&gt;The code you posted looks pretty good to me (I haven't tested it).  The problems I see with it though are increased call stack usage, and the ubbound for the character array loop is recalculated each time the IsWhitespaceCharacter function is called.  So my guess is it would result in a performance penalty.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=50885" width="1" height="1"&gt;</description></item><item><title>re: Can you optimize this code ? (Is GoTo evil ?)</title><link>http://msmvps.com/blogs/bill/archive/2005/06/06/50676.aspx#50882</link><pubDate>Tue, 07 Jun 2005 16:15:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:50882</guid><dc:creator>bill</dc:creator><description>I'm going to explain my thoughts first, then show the code since I wrote it without really knowing VB.NET and without a compiler:&lt;br&gt;&lt;br&gt;To me the first problem with your function is the duplication of code where you check if the first character is whitespace and where you check if the second character is whitespace.  If you pull that out into its own function that simplies the code and removes the need for the goto:&lt;br&gt;&lt;br&gt;Please excuse any obvious typos/mistakes.&lt;br&gt;&lt;br&gt;Public Function IsWhitespaceCharacter(ByVal charToCheck As Char, ByVal ParamArray whitespaceChars() As Char) As Boolean&lt;br&gt;   Dim whitespaceUbound As Int32 = whitespaceChars.Length - 1&lt;br&gt;   For j As Int32 = 0 To whitespaceUbound&lt;br&gt;       If charToCheck = whitespaceChars(j) Then&lt;br&gt;           Return True&lt;br&gt;       End If&lt;br&gt;   Next&lt;br&gt;   Return False&lt;br&gt;End Function&lt;br&gt;&lt;br&gt;&lt;br&gt;Public Function IsWhitespaceOnly(ByVal stringToCheck As String, ByVal ParamArray whitespaceChars() As Char) As Boolean&lt;br&gt;   Dim length As Int32 = stringToCheck.Length&lt;br&gt;   Dim middle As Int32 = length \ 2&lt;br&gt;   Dim first As Int32 = middle&lt;br&gt;   Dim second As Int32 = middle + 1&lt;br&gt;&lt;br&gt;   If whitespaceChars Is Nothing OrElse whitespaceChars.Length = 0 Then&lt;br&gt;      whitespaceChars = New Char() {&amp;quot; &amp;quot;c, ChrW(&amp;amp;H3000)}&lt;br&gt;   End If&lt;br&gt;&lt;br&gt;   For i As Int32 = 0 To middle&lt;br&gt;&lt;br&gt;      If first &amp;gt;= 0 Then&lt;br&gt;      	 If Not IsWhitespaceCharacter(stringToCheck.Chars(first), whitespaceChars) Return False;&lt;br&gt;         first -= 1&lt;br&gt;      End If&lt;br&gt;&lt;br&gt;      If second &amp;lt; length Then&lt;br&gt;      	 If Not IsWhitespaceCharacter(stringToCheck.Chars(second), whitespaceChars) Return False;&lt;br&gt;   	 second += 1&lt;br&gt;      End If&lt;br&gt;      &lt;br&gt;   Next&lt;br&gt;&lt;br&gt;   Return True ' I believe a empty string is considered whitespace only, correct as necessary.&lt;br&gt;&lt;br&gt;End Function&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=50882" width="1" height="1"&gt;</description></item></channel></rss>