home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!pipex!demon!edscom!jim
- From: jim@edscom.demon.co.uk (Jim Thomas)
- Subject: Re: SWITCH Statement Question
- In-Reply-To: u-mwong%peruvian.cs.utah.edu@cs.utah.edu's message of 15 Dec 92 19:24:55 GMT
- Message-ID: <JIM.92Dec18125732@runningbear.edscom.demon.co.uk>
- X-Disclaimer: #include <std/witty/disclaimer.h>
- Lines: 59
- Sender: jim@edscom.demon.co.uk (Jim Thomas)
- Reply-To: Jim Thomas <jthomas@edscom.demon.co.uk>
- Organization: EDS-Scicon, Milton Keynes, UK
- References: <1992Dec15.122456.1776@hellgate.utah.edu>
- Distribution: usa
- Date: Fri, 18 Dec 1992 12:57:35 GMT
-
- >>>>> On 15 Dec 92 19:24:55 GMT, u-mwong%peruvian.cs.utah.edu@cs.utah.edu (Michael Wong) said:
-
-
- >> 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. The following example will help clarify
- >> my question...
-
- >> 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. In other words, compared to:
-
- >> switch (key) { <50 CASE statements here> }
-
- >> would this following code be faster, if the SWITCH was broken down
- >> using IFs:
-
- >> if (...)
- >> {
- >> switch (key)
- >> {
- >> <10 CASE statements here>
- >> }
- >> }
- >> else if (...)
- >> {
- >> switch (key)
- >> {
- >> <10 CASE statements here>
- >> }
- >> }
- >> else if (...)
- >> <more SWITCH statements>
-
-
- >> I'm worried about speed, but I also want to keep the code easy to
- >> understand. I'd appreciate if someone could tell me exactly how SWITCH
- >> works internally!
-
- This is all compiler specific. I use Microsoft C and it handles switch
- statements very effeciently, building a combination of jumps and small
- sparse jump tables as required by the kind of cases you use.
-
- I have found that unless you know a lot about how the compiler works it
- is never a good idea to try and second guess it. If you add code to make
- it more effecient you may make it harder for the compiler to optimise
- your code.
-
- If speed or size is a problem after implementation then maybe look at
- stuff like this but otherwise try to make the code as readable as
- possible.
-
-
- --
- Jim Thomas, EDS-Scicon Ltd,Wavendon,Wavendon Tower,Milton Keynes,MK17 8LX,UK
- jim@edscom.demon.co.uk Tel: +44 908 284522
- Opinions expressed are my own, and are not necessarily those of my employer.
-