From: jestandi AT cs DOT indiana DOT edu (Jeff Standish) Newsgroups: comp.os.msdos.djgpp Subject: How is switch implemented? Date: 2 Apr 1997 13:41:48 -0500 Organization: Computer Science, Indiana University Lines: 31 Message-ID: <5hu99c$2t1@tuba.cs.indiana.edu> NNTP-Posting-Host: tuba.cs.indiana.edu To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp I'm currently working on a piece of code that is time critical, and could easily be done with a switch statement. What I'm wondering is whether it is safe to assume that in most circumstances, a compiler will implement the cases as an indexed look up (assuming that the values for the cases form a successive range, like 0 through 20, rather than a random collection of values), using the value of the case as an index into a table of jump addresses at the assembly level. This method would obviously be more efficient than a sequence of if-else's as I suspect would happen when the cases are essentially random values. I'm assuming that most intelligent compilers would try to use an jump table, but I don't know for certain, and I'm not in the mood to compile, disassemble, and try to read the machine code. Besides, just because this might happen for a trivial test program doesn't mean it will also work for 40+ cases spread out over several hundred lines of code (which is the minimum of what I expect to write). And I'm asking this in a more general sense, since in addition to DJGPP I also use Visual C++ (yes, yes, I know, spawn of the evil empire and all that...). Does anyone know enough about the internals of compilers these days to answer this one? Thanks Jeff -- ----------------------------------------------------------------------------- Jeff Standish http://www.cs.indiana.edu/hyplan/jestandi.html jestandi AT cs DOT indiana DOT edu May your deeds with sword and axe jlstandish AT acm DOT org Equal those with sheep and yaks