Which is faster? select case vs. lookup
June 16, 2010
So I am working on Walter's head moves which are working quite nicely. There are about 100 different moves stored in EEPROMS and can be cued-up and "played" via a number I have assigned to each move. I.e. I know that numbers 1-10 are all just sorta looking around moves. Move 47 is look quickly down and to the left, then a tilt. Etc. Etc. This leads me to the need to play some of the headmoves back at random. Not just at random, but I want one of the random picks to be part of a catagory of moves. Therefor I have a pack of "fwd looking around", a pack of "looking to the right", a pack of "looking to the left" etc. These numbers when put together, could almost be considered a string or array.
Also in the code, I have one byte (b19) clicking by... I have a inc b19 in many places of the code, mostly in the main loops. This variable is allowed to overflow and does nothing but count up, overflow and count up again. This is my randomizing number. Now, I know there is a random command but it sucks. Even if you seed it or bounce it off the timer, etc, it always has a pattern. With my "always changing variable" approch, I have very good, random results.
Now I might want to send a code to say "play a random fwd head move" and right now, I do this:
select case b19
case 1 to 23
case 24 to 46
case 47 to 69
case 70 to 92
case 93 to 115
case 116 to 138
case 139 to 161
case 162 to 184
case 185 to 208
case 209 to 230
'sertxd ("Ranfwd b13 ",#b13,CR,LF)
This returns b13 as a randomly chosen number between 1 and 21 (only odds --not important) and that number is used to call up a move. Also, this method allows me to change probabilty and favor or unfavor one or many moves.
Now I have been thinking of putting these numbers into a lookup table, converting b19 down to a number that could be used with the lookup command and getting my b13 that way. It seems that the lookup table would be faster (and for sure, cleaner to see written-out) but would it really be faster with the extra math at the begining? Is there a rule for calculating speed in these cases?
Did one single thing make sense at all in this whole post?