[LCP]Address of a label
Mike & Penny Novack
stepbystepfarm at shaysnet.com
Fri Feb 22 03:12:06 UTC 2002
Chirag,
Vincent has really hit he crux of this. Unless the (possible) values
of your switch represent a dense set this would be more trouble than
it's worth because every POSSIBLE value needs a destination (off course
all the "error" ones do the same thing). In other words, your array
would have to be as large as the "space" represented by ALL possible
values of the switch. Of course all us old assembler programmers know
about "computed jump tables".
Maybe you are asking the wrong question? (in terms of what you want
to accomplish). How different are your actions? The sense in which I
mean this in what way does "something 1" depend upon "switch value 1".
Rather than using the data to select the action, is there any way you
can make a single action "data dependent"?
The other thing to keep in mind is to look at run time efficiency
the correct way. Is this something done repeatedly? (if not, no point
trying to speed it up). The other is that run time efficiency does NOT
depend upon the number of lines of code. Let me explain that.
Here you have a "case statement" which is defined to process
sequencially. For 200 cases, 100 tests on average if all are equally
likely. BUT -- are they uniformly likely. If in fact the first couple
cases (in terms of the order you place them) are much more likely than
the others, then THAT determines the average behavior of the code ---
your optimization would gain little if "case1" were many times as likely
as all the rest combined. And there are other alternatives. Two hundred
possible cases CAN be done with 8 tests (it's less than 256) -- lots of
lines of coding expressed as a "binary if" but only 8 tests executed in
any one pass through.
Remember, the behavior of "case" is more general than a "jump
table" (you don't HAVE to have a "break" after each) which is why it
isn't implemented as one.
Mike
More information about the linuxCprogramming
mailing list