Jump to content
  • Advertisement
Sign in to follow this  
nyt3hawk

switch case question

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i've been reading though some source code to some older games and i've been coming across switch cases a lot. I was just wondering, is it basically the same as an if-else statement? is there any big reason for using them like speed or organization?

Share this post


Link to post
Share on other sites
Advertisement
I think they are mostly a syntactic shortcut for a giant if-else-if-else-if-etc-else structure. They can be easier for the coder to read for some kinds of tests.

I would think that they would be exactly as fast as an if-else structure, but I am not an expert.

Share this post


Link to post
Share on other sites
Quote:
Original post by rileyriley
I think they are mostly a syntactic shortcut for a giant if-else-if-else-if-etc-else structure. They can be easier for the coder to read for some kinds of tests.

I would think that they would be exactly as fast as an if-else structure, but I am not an expert.


It is possible for them to be faster than if and else, but I really doubt it makes that much of a difference. They also allow things like Duff's device (which most people consider horribly difficult to understand).

Share this post


Link to post
Share on other sites

int myVar = 2;

switch (myVar)
{
case (1):
Statements1;
break;

case (2):
Statements2;
break;

default:
Statements3;
break;
};

// Is the same as ...

if (myVar == 1)
{
Statements1;
}
else if (myVar == 2)
{
Statements2;
}
else
{
Statements3;
}



There are speed-up benefits -- IF USED CORRECTLY. Many programmers don't realize how to properly use switch, and in these instances it will actually perform more slowly than the equivalent if/else if. I won't go into the nitty-gritty assembly details (switch statements use jump tables whereas if statements use a sequence of comparisons), but here are some general guidelines:

(1) If <= 3 conditions to test for, if statements are generally faster or similar in performance.
(2) Switch statements are fastest if you have > 3 possibilities, most importantly the conditions you test for ARE SEQUENTIAL (i.e. 4, 5, 6, ...), and less importantly (only saves an add) your first sequence number is 0.
(3) The switch may also be worth it simply for readability's sake, for instance in finate state machines.

Share this post


Link to post
Share on other sites
I personally take switches because a huge clump of if/else gets pretty difficult to read. But like mentioned before, there are cases where it can be drastically better.

One case is VC optimizing sequential cases that all break and start with 0. Rather than all of that code being right next to eachother and only one path being executed (ignoring the majority of the code), VC uses a function-pointer lookup table.

So it doesn't even have to check a condition any more. It could just use the case as an index into the table.

But not all compilers do this and you really shouldn't rely on it because it could change at any moment. Definatly not part of the C++ Standard.

Also Duff's Device is pretty darn cool. That link explains it quite well, but Duff himself has a page on the topic that goes into a bit more detail.

A simple way to think about it is...the first time in the loop it just jumps (because of the switch) to a certain area in the loop. If you wanted it to happen 3 times, it would jump to 3 and not loop.

But if you wanted anything over 8 it would jump somewhere inside the loop...then when it goes to loop it is now a multiple of 8 (since it trimmed the extra off by jumping inside the loop). So anytime it executes the loop (should happen more than 8 times) it just jumps back up to the do line and continues.

Share this post


Link to post
Share on other sites
Quote:
Original post by nyt3hawk
i've been reading though some source code to some older games and i've been coming across switch cases a lot. I was just wondering, is it basically the same as an if-else statement? is there any big reason for using them like speed or organization?


Depends on the compiler if they are the same. Most (almost all) switches can be implemented via nesting if statements, because you can sort the cases and do a binary search. So, if you have 500 if statements that could complete in log(500) time with a switch/case rather then 500 for a sequence of it statements.

Having said that, I've looked at some code gen from compilers and been disapointed at how much optimization the compiler actually did.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!