"switch" under most compilers will generate a jump-table, so after evaluation it will take only one CPU instruction to get to the needed branch.
"if" is hardly optimized at all, so there is a huge chance that every if-condition will be tested.
(actually repeating hybrid)
As i remember a standard asm code by M.Abrash when he was discovering "if vs switch" there was a result: use "if ... else" statement only if you have less than 4 conditions (max example: if ... else if ...), in other cases use a switch ('cause 3 jumps better than jump-table for 3 cases, but j-table for 4 cases better than 4 if-jumps )
But in modern CPU's it itsn't critical problem, i think)
Also, it is best to eliminate as many branching conditions as you can in your code. Even with branch hinting, if the code is in a heavily used loop, it may create for a slowdown or bottleneck. (If too many are present.)
To make what I'm saying a little bit easier, say we have a conditional 'jpz' (jump zero) instruction. If the test case is true it executes in 7 cycles, but if the test case is not true it takes 12 cycles to execute.
That function has no external side effects and no return, so it will be the same. Both implementations will end up being an empty function.
If you are actually curious about this kind of thing, find out what compile flag you need to use to generate assembly, then use it. You really have to test for yourself because the answers will depend on the code, the optimization level, and the compiler.
Swarmer wrote:Switches are also much cleaner and easier to read in my opinion, which is just as important.
yep; the other big advantage to them is that if you change the name of the variable (or you put it inside of an object), you only have to change one line of code versus quite a few. I find myself doing this quite often when I'm first setting up a game
Would there be a way to adapt the switch statement to your movement code in your free flight demo?
(This code if nobody has any clue what I'm talking about)
// direction control
if(keys[irr::KEY_LEFT])
{
turn(shuttle, 0.25);
}
if(keys[irr::KEY_RIGHT])
{
turn(shuttle, -0.25);
}
if(keys[irr::KEY_UP])
{
pitch(shuttle, -0.25);
}
if(keys[irr::KEY_DOWN])
{
pitch(shuttle, 0.25);
}
if(keys[irr::KEY_COMMA])
{
roll(shuttle, 0.45);
}
// And so on...
The Open Descent Foundation is always looking for programmers! http://www.odf-online.org
"I'll find out if what I deleted was vital here shortly..." -d3jake
No there is no particular, easy, way to do that, and you wouldn't want to do that. That code is engineered that way so as to allow multiple key presses from a boolean array. Thus the user can press up and left, and it will result in an up and left movement.
That code is engineered that way so as to allow multiple key presses from a boolean array. Thus the user can press up and left, and it will result in an up and left movement.
Exactly. Also user input is not going to happen much too often (maximum once per loop). You do not have to struggle for such subtle optimization. You need that in parts of code which are going to be called say few dozens or hundred times per loop.