Related articles |
---|
methods used by compilers for switch{ case } statements? mathog@seqaxp.bio.caltech.edu (2000-03-03) |
Re: methods used by compilers for switch{ case } statements? ast@halcyon.com (Andrew Tucker) (2000-03-06) |
Re: methods used by compilers for switch{ case } statements? rhyde@shoe-size.com (Randall Hyde) (2000-03-06) |
Re: methods used by compilers for switch{ case } statements? lehotsky@tiac.net (Alan Lehotsky) (2000-03-06) |
From: | "Randall Hyde" <rhyde@shoe-size.com> |
Newsgroups: | comp.compilers |
Date: | 6 Mar 2000 01:09:53 -0500 |
Organization: | Compilers Central |
References: | 00-03-006 |
Keywords: | code |
<mathog@seqaxp.bio.caltech.edu> wrote
> Can someone please point me to an online reference which describes the
> mechanism(s) compilers use to implement the switch/case statement in
> C? Specifically, which method or methods does the object code
> implement? I'm also interested in references to any discussion
> comparing the speed of a small switch statment (say 2-3 cases ) versus
> the equivalent logic implemented with if statements. At what N does:
>>>Snipped.
See Chapter Ten of "The Art of Assembly Language Programming".
You can find this at http://webster.cs.ucr.edu.
This text discusses jump table implementations, if/elseif implementations,
and some x86 specific implementations (e.g., using a LOOP instruction).
The speed of the implementation depends entirely on the CPU in use
(it varies considerably amongst x86 CPUs, probably even more so across
different architectures). Generally, though, the speed trade-off seems to
be around 3-4 cases assuming the jump table is in cache memory.
Of course, table lookups tend to run *very* slowly if the data is not
cached,
so if the switch statement rarely executes (or executes infrequently enough
that it gets bumped from the cache), then that's not the right way to go.
Of course, if the switch doesn't execute very often, who cares how fast
it is? (other than a compiler writer who won't know how often it executes
:-)
Randy Hyde
Return to the
comp.compilers page.
Search the
comp.compilers archives again.