Skip to main content
gamedev.net
🔒 Locked

switch or if?

Started by thehurricane 12 replies 350+ views
Original Post
thehurricane
thehurricane
Which is better, switch statements or lots of ifs? Specifically, which is more efficient? I know I prefere the structure of switch, but the drawback is that you can easily forget a break statement.
groby
groby
None is ''better''. In case of having a lot of different conditions, a switch is simply more readable. (If those conditions are constant, that is.)

As for efficiency - I''d be surprised if that really mattered. On a good compiler a switch might be just a smidgen faster, but if your core loops require huge ''if'' or ''switch'' statements, I suggest you revisit your algorithm instead and find a way to write it without conditionals at the core level. (Why is switch faster? Because smart compilers do range checks and/or binary search on switch labels, if appropriate. If you do it by hand, you might overlook a case)

Finally, for the missing break: C++ stinks. The only way to keep this sort of under control is always having a default: label that does nothing except print an error message and stop. At least you''ll find it easier that way.

(For the language advocates out there: Although C++ stinks, it''s still one of the best languages available for core game engine routines. Which tells you a lot about the state of computer languages....)

Hope this helps,
- Robert
kuphryn
kuphryn
For multiple options, I recommend a map over a switch.

Kuphryn
outRider
outRider
Not having a break can be pretty damn useful, it's not a negative that you require a break to prevent fall through, or else I'm sure someone would have made it a point to change the language specs. Not counting Duff's Device, not using breaks for cases that are handled uniformly makes your source that much more readable, that much shorter in terms of lines, and you can really make use of it in good ways.

Besides, for lots of cases it's a hell of a lot better than 20 nested ifs that indent themselves right off your screen.

------------
- outRider -

[edited by - outRider on December 15, 2002 8:22:05 PM]
Ready4Dis
Ready4Dis
AP: Perfect example of re-writing your core logic. Instead of using huge conditions for your byte code, you can simply store a large function array that stores each bytes function (the function that each byte stands for), and in each function, it uses the current byte location in the program to read any aditional information that function requires (so there is no passing of params to said functions, therefore, no conditional statments at all).

I don''t beleive there is a difference in speed, whichever one you are more comfortable with.

Billy - BillyB@mrsnj.com
DerekSaw
DerekSaw
IMHO, if it has more than one options, plus it''s an integer, and only if for equivalence comparison then I suggest using switch would look much cleaner and clearer.

  
if (nOption == PLAY)
{
}
else if (nOption == SAVE)
{
}
else if (nOption == LOAD)
{
}
// look at these:

switch (nOption)
{
case PLAY:
...
break;
case SAVE:
...
break;
case LOAD:
...
break;
}
"after many years of singularity, i'm still searching n the event horizon" o
Zipster
Zipster
I was decompiling some switch code, and apparently if the input number was greater than all the cases, it automatically knew to go to the default case. Now that''s just sweet
Qw3r7yU10p!
Qw3r7yU10p!
if you're using c++ then there are situations where it is good t replace switch statements or conditional statements with polymorphism.

these techniques are well described in 'refactoring' by martin fowler. it's worth the money and there are also many articles on the web.

if your switch is causing different functionality or code to be called then there's a chance you could move the switch code to being an object and derving classes to make different strategies. if you often make switches based on the same set of types then that's when it starts to smell bad.

edit: what you end up with is a set of objects representing the desired variations in functionality. they get created using a switch statement and other than that you don't need to make those decisions any more. so the switch stays in your factory and that's it.

[edited by - petewood on December 16, 2002 6:45:40 AM]
AndyOxfeld
AndyOxfeld
quote:
Original post by petewood
if you''re using c++ then there are situations where it is good t replace switch statements or conditional statements with polymorphism.


I think the debate in this thread has mostly been whether switch or if is faster in time-critical code... and everyone knows polymorphism is SLOW! Polymorphism can definitely be useful, but I disagree with suggesting that simple and easy if/switch statements be replaced with complex and slow polymorphism.

- Andy oxfeld
SabreMan
SabreMan
quote:
Original post by AndyOxfeld
I think the debate in this thread has mostly been whether switch or if is faster in time-critical code... and everyone knows polymorphism is SLOW!

That''s a common piece of ignorance often stated on these forums. If you need polymorphic behaviour, then you need polymorphism. An alternative to C++''s runtime despatch is highly unlikely to be quicker.
quote:

Polymorphism can definitely be useful, but I disagree with suggesting that simple and easy if/switch statements be replaced with complex and slow polymorphism.

It''s not complex. It''s a lot simpler to have the compiler perform tasks on your behalf than it is to manually create some half-assed alternative. Neither is it slow - if you have a quicker alternative that does the job, then you clearly don''t need polymorphism in the first place. It''s people who don''t understand polymorphism, and hence end up abusing it, who come out with that sort of claim.
Qw3r7yU10p!
Qw3r7yU10p!
Hi Andy, I can see what the debate has been about. But actually the original question did have an underlying point about the difficulty of maintaining the code which is down to how easy it is to understand what your code is doing, how it varies and where that variation takes place, not to do with efficiency.

quote:
original poster: Which is better, switch statements or lots of ifs? Specifically, which is more efficient? I know I prefere the structure of switch, but the drawback is that you can easily forget a break statement.


We all know nested ifs are a headache. We all know that if you miss a break it can be a really hard and stupid bug when you find it (aren''t many if not all bugs stupid when you find them?).

The OP is worried about efficiency but also is concerned about the reliabilty of their code and is aware of their own falability. I''m addressing those concerns.

PS Polymorphism is an idea or concept! I guess how quick it is depends on how quickly you can think.
thehurricane
thehurricane
Thanks all--my main issue was basically flat-out speed, since it will be at the heart of my game''s core loop. I''m also aware that compilers are smart enough these days to usually even out things like this--I was just curious about the machine code differences and which is considered more "efficient." It sounds like switch is the way to go--I''ll just have to watch those damn breaks and deal with it

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.