Thursday 2 November 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...