Micro Performance optimisation using If-Else


This post is actually a question I asked on Stackoverflow,

The question is Which performs better if or if-else ?, this might seem to be a silly question for a few because of 2 reasons.

  • If block will have relatively less lines of code, if both the blocks are having the same code, it is apparently a matter of commonsense.
  • And if they are not having the same code then we should not compare them in the first place

So Which one actually performs better, well lets look into the  results

When Condition is true, the If Block took 1700 milliseconds to execute the 1000000 iterations, where as the if-else block took only 1688 milliseconds

When the conditions fails, the if scored 1677 Milliseconds which is still a bit late than the If-Else block’s 1664 Milliseconds

So the results  say that If-Else performs better than If and guess according to most of SO posters the If-Else block is more readable too

Advertisements