Thanks Robert,
I've seen the disassembled output and thought (in my case)
it could be tuned to be faster but I just hadn't found a way.
My hashing algorithm was too slow.
I think your method will be faster and hopefully the empirical
data will match my belief.
W.
-----Original Message-----
From: Robert Purves [mailto:robert.purves@...]
Sent: Wednesday, April 24, 2002 1:12 AM
To: futurebasic@...
Subject: Re: [FB] Fast lookups and jumps
>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.
>
> Ultimately, I want to see if I can gain
>enough speed to warrant some ugly inflexible code.
>
>SELECT myString$
>CASE "BD"
>// do this ...
>CASE "LI"
>// do this ...
>CASE "SIMM"
< snip >
>END SELECT
Case with a string is slow because there are two calls to runtime routines
(GETSTRCONST and FBCompareStrings), as shown by this disassembly:
CASE "BD"
' +00000014 li r3,0x0B12
' +00000018 bl GETSTRCONST+0x0004
' +0000001C li r31,0x0021
' +00000020 mr r3,r31
' +00000024 bl FBCompareStrings+0x0004
' +00000028 mr. r3,r3
' +0000002C bne _FCOD_1001+0x0048
' +00000030 b _FCOD_1001+0x004C
Convert your string's 1-4 characters to a long, and use underscore
constants in the case statements:
dim as ptr myStringPtr
dim as long myLong, stringLen
myStringPtr = @myString$
myLong = 0
stringLen = myStringPtr.nil``
while stringLen
stringLen--
myStringPtr++
myLong = (myLong << 8) + myStringPtr.nil``
wend
select myLong
case _"BD"
case _"LI"
case _"SIMM"
end select
Case is now completely inline, and much faster:
case _"BD"
' +000000F4 li r3,0x4244
' +000000F8 lwz r4,0x0020(SP)
' +000000FC cmpw r3,r4
' +00000100 beq _FCOD_1001+0x011C
' +00000104 b _FCOD_1001+0x0120
Robert P.
--
To unsubscribe, send ANY message to <futurebasic-unsubscribe@...>