home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!charnel!sifon!thunder.mcrcim.mcgill.edu!mouse
- From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
- Newsgroups: comp.lang.c
- Subject: Re: SWITCH Statement Question
- Message-ID: <1992Dec17.221738.7740@thunder.mcrcim.mcgill.edu>
- Date: 17 Dec 92 22:17:38 GMT
- References: <1992Dec15.122456.1776@hellgate.utah.edu>
- Distribution: na
- Organization: McGill Research Centre for Intelligent Machines
- Lines: 51
-
- > Distribution: usa
-
- Sigh. I'm going to have to play with some scripts and complain to our
- newsfeeds about their passing us bogus distributions...
-
- In article <1992Dec15.122456.1776@hellgate.utah.edu>, u-mwong%peruvian.cs.utah.edu@cs.utah.edu (Michael Wong) writes:
-
- > I have a question regarding the SWITCH statement in C. I'm having
- > conflicting answers from people at work and need some help. How does
- > the SWITCH statement process the CASE statements? It's not clear to
- > me how it works internally.
-
- It varies, depending on machine, system, compiler, the sparsity of the
- labels, and possibly other factors such as optimization tradeoff
- options specified at compile time.
-
- > Will using a SWITCH statement run faster if it had few CASE
- > statements and IF statements were used to aid in the processing than
- > using a SWITCH statement that had a very long list of CASE
- > statements[?]
-
- Maybe. It depends on machine, system, compiler, the sparsity of the
- labels, and possibly other factors such as optimization tradeoff
- options specified at compile time.
-
- A good compiler, told to optimize for speed, should make this
- transformation automatically if it would speed up the resulting code
- without changing the semantics.
-
- > I'm worried about speed, but I also want to keep the code easy to
- > understand.
-
- Then write it whatever way is clearest. If the time cost of the switch
- turns out to be significant (not "is thought to perhaps maybe be
- significant"), and the compiler you're using doesn't make simple
- transformations that would speed it up, then (a) start looking for a
- better compiler and (b) put in some sort of system-specific conditional
- compilation in the meantime.
-
- > I'd appreciate if someone could tell me exactly how SWITCH works
- > internally!
-
- Go read your compiler documentation, or failing that source, or failing
- that go ask your compiler vendor. If you want to know about some
- specific example, look at the generated code, but be careful not to
- jump to conclusions.
-
- der Mouse
-
- mouse@larry.mcrcim.mcgill.edu
-