1

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 >= ?

vipin agrahari
  • 2,691
  • 3
  • 21
  • 31
  • Yes. The second one does one more iteration. – Maroun Dec 16 '15 at 06:26
  • 6
    It has nothing to do with performance but with logic. – Yassin Hajaj Dec 16 '15 at 06:26
  • 3
    @MarounMaroun Depending on where you start.. – Yassin Hajaj Dec 16 '15 at 06:27
  • I had heard in college that comparing with zero is more performant. So `i>=0` should be performant than `i>-1` I don't have any explanation for it though – sidgate Dec 16 '15 at 06:27
  • @sidgate we generally leave it up to the compiler to determine which is best to use for the target architecture, and in the code we try to use the best logic to express the problem being solved. – paddy Dec 16 '15 at 06:28
  • 2
    @YassinHajaj that's true. Since OP is comparing them I assumed they're the same. – Maroun Dec 16 '15 at 06:29
  • 1
    @sidgate In C, the generated assembly code for comparing to zero is a simple sign-check instruction (on x86 processors anyway), where comparing to other value has to actually compare. In Java, JIT may do the same, but it's unlikely to be meaningful, since you have no control over JIT. – Andreas Dec 16 '15 at 06:30
  • If `x` is a variable, there is probably a performance difference between `i < x` and `i <= x-1` (which are logically equivalent unless something wraps). The subtraction takes a bit of time. However, if this is a loop termination condition and `x` is a local variable, a good compiler may be able to tell that `x` cannot be changed during the loop, and do the subtraction only once. – ajb Dec 16 '15 at 06:35
  • From my experience, `i < x` is more commonly used than `i <= x` because you're usually loop over elements in an array. If you did `for (int i = 1; i <= x; i++)` where x is the length of the array, you would have `i-1` scattered throughout the body of your code. If you did `for (int i = 0; i <= x -1; i++)`, you would have the same loop body but in that case, `for (int i = 0; i < x; i++)` looks a lot cleaner. If there is any performance boost from `i < x` as opposed to `i <= x`, my assumption would be that any modern compiler would optimize `i <= x`and your code to something similar to `i < x` – hargasinski Dec 16 '15 at 06:39

3 Answers3

4

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.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Is there any difference in execution time required for both operations. i.e. > and >= – vipin agrahari Dec 16 '15 at 06:31
  • 1
    For the raw boolean expression itself? If there is, it's ***very*** insignificant, and if you're hoping to optimize your platform based on that sort of decision, there are *other* areas you should look to. – Makoto Dec 16 '15 at 06:32
  • @vipinagrahari The execution time of `<` vs `<=` is the same. The execution time of iterating the loop one more time is of course directly affected by the time an iteration takes. – Andreas Dec 16 '15 at 06:32
  • 1
    Isn't it frustrating when you answer a question and you get asked the same question again, as if you didn't even answer it... – paddy Dec 16 '15 at 06:33
  • @paddy I am sorry. But I actually could not understand if there is any actual difference. It will be great if you can give some reference. – vipin agrahari Dec 16 '15 at 06:35
  • @vipinagrahari: I'd hope to clarify, but I'm not sure I can. The difference is insignificant, if it exists at all. – Makoto Dec 16 '15 at 06:36
1

No, it makes absolutely no difference. When the compiler turns it into assembly, it turns both of them into the same cmp instruction.

occamsrazor
  • 105
  • 8
0

There are times you prefer to use <= over <, for instance in a for loop where usually we go from 0 to length-1 (eg an array length), we use <. But if you want to include the compared value as well, here y, and keep the < logic, you get

for (x=0 ; x<y+1 ; x++) {
    ...
}

while doing this

for (x=0 ; x<=y ; x++) {
    ...
}

saves a +1 calculation.

Besides that, most processors have both the less than and less or equal instructions - thus using one or the other has no impact on performance.

Déjà vu
  • 28,223
  • 6
  • 72
  • 100