Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!caip!topaz!ll-xn!nike!lll-crg!seismo!umcp-cs!cvl!umd5!zben From: zben@umd5 (Ben Cranston) Newsgroups: net.lang.c Subject: Re: Generating code for the switch statement Message-ID: <1171@umd5> Date: Wed, 6-Aug-86 19:26:26 EDT Article-I.D.: umd5.1171 Posted: Wed Aug 6 19:26:26 1986 Date-Received: Sat, 9-Aug-86 02:19:24 EDT References: <15093@ucbvax.BERKELEY.EDU> <2765@brl-smoke.ARPA> <15120@ucbvax.BERKELEY.EDU> <610@hropus.UUCP> Reply-To: zben@umd5.umd.edu (Ben Cranston) Organization: University of Maryland, College Park Lines: 29 Summary: The "three times" got me once... In article <610@hropus.UUCP> ka@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"