Jump to content
  • Advertisement
Sign in to follow this  
King of Men

Optimisation : If-else, or just if?

This topic is 5135 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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
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_else

else_branch:
execute else-branch

end_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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
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.
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.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!