Optimisation : If-else, or just if?
This is an optimisation question, and I know that it's a totally useless optimisation - I'm just curious. Which is more efficient :
if (enemy == null) return;
enemy.getShot(this);
or
if (enemy == null) return;
else enemy.getShot(this);
Perhaps a modern compiler produces equivalent machine code anyway? Or is (jump to next line) equally efficient to (jump to line x, x = next)? Or could you really save a nanosecond by choosing one option? I'm dying to know!
It depends on how smart the compiler is; if the compiler isn't very smart the first one will be a little faster, but not by much.
They should generate identical machine cose, since in both cases the if returns, and when the if is false it jumps around the 'return'.
If there is a speed difference, you _NEED_ to change compilers because your compiler has 'reverse optimization' or something where it makes the code worse.
If there is a speed difference, you _NEED_ to change compilers because your compiler has 'reverse optimization' or something where it makes the code worse.
This is micro-optimisation, anyway so I wouldn't even bother...
[edit]
Sorry, missed that first line of yours [smile]
As the others said - it makes no difference.
[/edit]
[edit]
Sorry, missed that first line of yours [smile]
As the others said - it makes no difference.
[/edit]
The compiler will produce better code for branch prediction if you put the common case first. So, if you typically have an enemy, if (enemy != null) might be faster.
It really shouldn't make a difference, no matter what the compiler does. All the else statement does is introduce a jump label for the if-branch to jump over after it has been executed.
so essentially, all you do by omitting a (useless) else statement is getting rid of one goto and the subsequent else-branch part. You could see measurable results if you manage to get this into a very tight loop and fool the CPUs branch prediction. But this is highly unlikely since the goto inside the if-branch is non-conditional.
if statement is true then goto if_branch goto else_branch;if_branch: execute if-branch goto end_elseelse_branch: execute else-branchend_else: ...
so essentially, all you do by omitting a (useless) else statement is getting rid of one goto and the subsequent else-branch part. You could see measurable results if you manage to get this into a very tight loop and fool the CPUs branch prediction. But this is highly unlikely since the goto inside the if-branch is non-conditional.
you should in 99% of the cases never use an if in a time critical situation, so this should never bother you, if you end up with an if in an innerloop you are on deep water anyway, try rethink your algorithm.
Quote:Original post by Anonymous PosterWhile this might be true in low powered systems, on the PC this kind of thing is really no longer a rule. Just about every time critical system in a game NEEDs conditional brances to do its job. It is a good idea to limit them and keep them simple, but it is impossible to eliminate them in many situations.
you should in 99% of the cases never use an if in a time critical situation, so this should never bother you, if you end up with an if in an innerloop you are on deep water anyway, try rethink your algorithm.
Unless you're talking about realtime in a way entirely unrelated to game programming (as in 'realtime mission critical systems' whose failure could cost real lives), in which case it isn't really relevant.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement