Saturday, 12 August 2017

java - Is there any performance difference between using > and >= in a loop




In loops we keep terminating conditions and we check those conditions in every pass.



I have seen 2 methods to check




1 . i > x or i < x



and the second approch is



2 . i >= x or i <= x



Is there any performance difference in these to 2 approaches while the logical comparison.



Is there any difference in execution time required for both operations. i.e. > and >= ?


Answer




There's very little if any performance difference between these two statements, but there is a significant difference between the two statements and their logic flow.



For instance:




  • If you have a loop that runs until i <= 20, you'll loop until i == 20.

  • If you have a loop that runs until i < 20, you'll loop until i == 19.



If you have a condition that requires your iteration value to stop before a certain value, be sure that you pick the inequality that most suits it.




By and large, though, if there were any difference in run time or performance on this boolean statement, it would be barely noticeable. One should not base any optimization work around switching out those statements.*



*: Also, don't break Rule #1 of Optimization.


No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...