Generating code for the switch statement

Ben Cranston zben at umd5
Thu Aug 7 09:26:26 AEST 1986


In article <610 at hropus.UUCP> ka at hropus.UUCP (Kenneth Almquist) writes:

> If the cases are not consecutive, the branch table must be filled with
> entries for the "missing" cases, making the table larger.  The Ritchie
> compiler will generate a branch table if the number of entries in the
> branch table would be less than three times the number of cases.  

This bit me once.  In one of an innumerable number of Impress-understanding
programs I converted an IF nest into a switch statement, and was chagrined
when the program didn't show a vast efficiency improvement.  After peeking
at the (BSD4.x) C compiler code generator we found the 1/3 fudge factor, 
and that the Impress codes were only two short of being dense enough to 
compile into a jump table.  We didn't actually try this, but I suppose that 
if we had defined two or three bogus Impress codes and put in cases for 
them then the switch statement would have suddenly started being much more 
efficient.

Which brings me to another point: many people seem to make their programs
"more efficient" by minimizing the size of the (higher level language)
SOURCE code, rather than minimizing the output object.  Usually these go
hand-in-hand, but the above is an interesting case where MORE source code
resulted in LESS (and FASTER) object code...

And we won't even TALK about MOVE CORRESPONDING or SORT...

-- 
                    umd5.UUCP    <= {seismo!umcp-cs,ihnp4!rlgvax}!cvl!umd5!zben
Ben Cranston zben @ umd2.UMD.EDU    Kingdom of Merryland Sperrows 1100/92
                    umd2.BITNET     "via HASP with RSCS"



More information about the Comp.lang.c mailing list