# Optimisation : If-else, or just if?

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

## Recommended Posts

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!

##### Share on other sites
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.

##### Share on other sites
What would a dumb compiler do, that's different from what a smart compiler would do?

##### Share on other sites
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.

##### Share on other sites
This is micro-optimisation, anyway so I wouldn't even bother...

Sorry, missed that first line of yours [smile]
As the others said - it makes no difference.
[/edit]

##### Share on other sites
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.

##### Share on other sites
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.

  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.

##### Share on other sites
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.

is this java?

##### Share on other sites
Quote:
 Original post by Anonymous Posteryou 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.
While 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.

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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
khawk
11

• 9
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013299
×