[futurebasic] Fast lookups and jumps

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : April 2002 : Group Archive : Group : All Groups

From: "Edwards, Waverly" <Waverly.Edwards@...>
Date: Tue, 23 Apr 2002 18:44:16 -0400

I have the need for speed.  Does anyone have a 
better idea for a jump table to replace a SELECT/CASE
structure?

What I would like to do is to jump to a certain section 
based on the contents of a string.  The string will be one 
to four characters in length, all capitals.

What I am doing now is easy and flexible but 
I think there may be a faster, less flexible way to do 
the same thing.  Ultimately, I want to see if I can gain
enough speed to warrant some ugly inflexible code.

This is what I do now.

SELECT myString$
CASE "BD"
// do this ...
CASE "LI"
// do this ...
CASE "SIMM"
// do this ...
CASE "XY"
// do this ...
CASE "D"
// do this ...
....
....
....
// THERE ARE ABOUT 40 CASE STATEMENTS
// ORDERED BY FREQUENCY FOR SPEED.
....
....
END SELECT

I get good speed now but I what I would like to do is something like
using an array as a lookup table


hashVal = FN hashString(myString)
 // hash value is the index into array

jumpAddr = myAddrLookup(hashVal)
// array will need to be large and mostly
// empty to support a direct relationship
// of hash value to index

goto jumpAddr

"BD"
// do this ...
EXIT FN
"LI"
// do this ...
EXIT FN
"SIMM"
// do this ...
EXIT FN

The problem here is that it would take longer to compute the hash value
than it would to go through the select statements.  Unfortunately I cant
do this 

hashVal = [@myString +1] 

because the string length is variably one to four characters

I cant do this

select myString[0]  // string length
case 1
   hashVal = myString[1]
case 2
   hashVal = myString.1%
case 3
   // cant do three at all
case 4
  hashVal = myString.1&
end select

because I use up time just testing the string length before I get to the
table lookup.

I'm willing to sacrifice memory, inflexible and ugly code in order to
test the idea of getting nearly instantaneous jumps but none of the 
ideas I have are fast.

Any ideas?


W.