[2 htp] Jump table. Long switch statements can slow down program
execution. For example, if one is executing some loop many times and each
time one must select one case out of 60 possible cases. This will take 30
comparisons in average, and each comparison involves at least 2 machine
instructions!
If the selection values are integer valued, starting from zero and making
up more or less contiquous value set, then one can use a jump table instead
of many if-then-else type comparisons. With jump table one can reach the
right case in a fixed time with just a few machine instructions. Jump table
contains many jump instructions (one per case), of which one is selected
with indexed addressing mode. For example, if register R3 contains the
selection value, and jump table address is JTBL, then the right jump
instruction is selected in the jump table with machine instruction
"JUMP JTBL(R3)".
- Give a (Koksi executable) ttk-91 example on jump table use, when there
are 10 cases to select from.
- How does one implement the switch statement above, if the jump table
contains only the case addresses instead of the jump instructions
leading to each case? Give the implementation for the same example as in
part (a).
- When is type (a) implementation better than type (b) implementation?
Why?
- When is type (b) implementation better than type (a) implementation?
Why?
- If each case takes 10 instructions and there are 60 cases, then how
much (%) does the use of jump table speed up the execution (as compared
to multiple comparison approach)? Is there a difference between
implementations (a) and (b) in this respect?