Hi all, Is it efficient on the computer's resources if I place an if statement inside a switch statement? The reason I ask is because for whatever reason, I can't get my else clause to work. Switch, on the other hand, works just fine. Jes __________ View the list's information and change your settings at //www.freelists.org/list/programmingblindMost i86 based C compilers generate code that converts all of the case statements inside a switch to a set of integers that it sorts from top to bottom and looks like:
mov esi, numCases mySwitch: move doSomething, doSomethingElse[esi] sub myValue, internalValue jz dosomething dec internalValue jz mySwitch jmp endSwitch doSomething0 proc ; do SomethingUseful jmp endSwitch doSomething[1] proc ;Do something else useful jmp endSwitch [[repeat for all cases] endSwitch: [doTheRestOfYourProgramThis can be made more efficient by using an xlat instruction but, as of the last time I looked, few compilers had that operation.
So, adding an "if" statement to a switch would simply put some sort of compare instruction into one of the cases which, if an integer for instance, would just looklike:
sub myInteger, theItemToCompareWith jnz notEqual [Do something that you want your if to do.] jmp endSwitchHistorically, compare operations (cmp) were considered slow (I think they took 3 or 4 cycles on a box with an 80286) so we tried to avoid them in tight loops. Now that a cycle takes roughly one third of a billionth of a second, even if it is still a slow instruction,it doesn't really matter much where you use it. Do not mistake a switch with a tight loop. A switch is really efficient for a compiler to optimize but sometimes loops that try to do too many things too tightly are the cause of what will get your profiler screaming bloody murder.
Happy Hacking, cdh __________View the list's information and change your settings at //www.freelists.org/list/programmingblind