The D Programming Language: switches

j.r.lupien jrl at anuck.UUCP
Fri Mar 25 04:51:15 AEST 1988


>From article <18377 at think.UUCP>, by barmar at think.COM (Barry Margolin):
> In article <1186 at PT.CS.CMU.EDU> edw at IUS1.CS.CMU.EDU (Eddie Wyatt) writes:
>> >>Yes, in a sense switch is less powerful than an else-if chain.  Let's keep it,
>> >>for the same reasons that we retain flow constructs less powerful than goto.
>> > 	Theoretically the switch construct is more powerful than an else-if
>> > chain because it selects in one step.  Execution is O(1) rather than O(n)
>> > where n is the number of branches. 

  Things get even better than this. It is possible to define parameters
of switch selection which allow even more optimization. These parameters
involve the number of switch values and the range of values. For any
given machine architecture, certain ranges of these parameters will
perform better using one of: indexed dispatch table, linear search
dispatch table, and hashed dispatch table. The compiler can count
the number of entries, examine the spread, and select the best 
implementation for each switch. 
   Naturally "best" means slightly different things to different 
programmers: some need speed, some need space, some need some arcane
relationship between speed and space. Building in a means for the 
programmer to tell the compiler which feature is more important and
by how much would permit the compiler to make even better choices
for such cases as switch coding. (for instance, MSC 5.0 has 
optimization switches to allow selection of speed or space optimazation)

John R. Lupien
twitch!mvuxa!anuxh!jrl



More information about the Comp.lang.c mailing list