**Repost from performance** Select Case long expre

Results 1 to 2 of 2

Thread: **Repost from performance** Select Case long expre

  1. #1
    Join Date
    Dec 1969

    Default **Repost from performance** Select Case long expre

    I posted this question earlier to the ASP Performance Forum in thread: <BR><BR>http://www.aspmessageboard.com/forum/performance.asp?M=373232&T=373232&F=23&P=1<BR><BR> but I realized later that it might not get a response there since it is not a frequently visited section.<BR><BR>My question was:<BR>Select Case allows each Case clause to be an expression list. <BR><BR>Example: <BR><BR>Select Case "strSomeString" <BR>Case "a", "b", "c" <BR>Response.Write "It is a-c" <BR>Case "x", "y", "z" <BR>Response.Write "it is x-z" <BR>Case Else <BR>Response.Write "it is something else" <BR>End Select <BR><BR>How does this effect performance? Is it quicker or slower than putting each value its own clause? <BR>

  2. #2
    Join Date
    Dec 1969

    Default In theory...

    It should make no difference.<BR><BR>I know how a C/C++/Java compiler would handle that:<BR><BR>It would build a table of values vs. address-of-the-relevant-Case.<BR><BR>If the values are adjacent (remember, those languages only let you use integers for the case values) or nearly so, then they don&#039;t even put the values into the table (they just index into it at the appropriate spot). If the values are non-adjacent (as they certainly are when you are using strings, as here), then either a hash table or a binary tree is used, to make the search for the matching value fast.<BR><BR>Since it doesn&#039;t matter whether you use multiple values for a single case or put each one in its own case so far as the construction of the lookup table is concerned (after all, the only other thing in each table entry is the address/offset to the code that implements that case), it should never make any difference.<BR><BR>HAVING SAID ALL THE ABOVE: Actually, I don&#039;t think that the VBS byte code is that smart. I think that VBS simply turns your SELECT CASE into a series of "if...elseif...elseif..." equivalents, with a "jump to code" when a match is found. (It&#039;s been two years since I looked at this part of the VBS source code, and that was older source, so don&#039;t quote me on this.) Assuming I am right, then this means you get a *tiny* performance boost if you move the most frequently encountered cases to the first. (But it still won&#039;t matter if you use multiple cases or not. And it truly is tiny, as the if...elseif code is being done at machine code speed.)<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts