3

Both of the following for-loops will be executed N+1 times:

for(int i = 0; i <= N; ++i);
for(int i = 0; i < N + 1; ++i);

Which of the two expressions (i <= N or i < N + 1) is faster to compute? I know there is a popular similar question (Is < faster than <=?), but I think this is different because here we are adding 1 to a variable, that might not be constant, and then comparing it to i, not comparing it to a constant value.

DimK
  • 301
  • 4
  • 16
  • 9
    I wouldn't be surprised if they result in the same assembly. – tkausl Dec 17 '17 at 03:01
  • Compilers are smarter than what you assume. If you want to know, try with your own timer or use a profiler. In any case, your `N + 1` will not be computed N times, so I don't see the point... – RaphaMex Dec 17 '17 at 03:08
  • Write for clarity. Not for micro-optimizations. Measure befroe trying to optimize. – François Andrieux Dec 17 '17 at 03:10
  • _Results Will not be same_ i think in Every Language . @tkausl – Syed Zain Ali Dec 17 '17 at 03:12
  • 1
    The two expressions are not quite identical if `N` is not a constant value; `N + 1` might overflow. – Keith Thompson Dec 17 '17 at 03:13
  • 3
    Either loop could be optimized away completely. The body of the loop does nothing, and `i` is local to the loop so its value is irrelevant. Compilers can be both more clever and more stupid than you expect them to be. The C++ language says nothing about the relative performance of the two loops. Your question is really about the performance of the code on the implementation you're using, and the only way to answer that is to measure it. – Keith Thompson Dec 17 '17 at 03:15
  • Possible duplicate of [Is < faster than <=?](https://stackoverflow.com/questions/12135518/is-faster-than) – Jonathon Reinhart Dec 17 '17 at 03:17
  • @KeithThompson OK, I understand. Thank you. – DimK Dec 17 '17 at 03:17
  • @JonathonReinhart I explicitly explained why it isn't. – DimK Dec 17 '17 at 03:18

1 Answers1

5

First, if N is constant, the compiler computes N+1 at compile time, and generates the same number of instructions for both options. The case of variable-vs-constant comparison is explained well in this Q&A.

When N is a variable whose value is available only at run-time, a compiler running at an aggressive level of optimization could produce the same code for both comparisons, too.

I ran an experiment with gcc running at -O3 optimization level, giving it these two code fragments:

scanf("%d%d", &j, &k);
if (j < k+1) {
    printf("hello\n");
}

and

scanf("%d%d", &j, &k);
if (j <= k) {
    printf("hello\n");
}

I used scanf to prevent the compiler from optimizing the expression entirely.

Assembly code produced in both cases was identical:

movl    -8(%rbp), %eax
cmpl    -4(%rbp), %eax
jg      LBB0_2

it compared j to k, and jumped over the call of printf (which optimizer replaced with a call to puts) when j was greater than k.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 2
    Good job! Never assume what compilers do. Just check it. It could compile differently for N=16 and for N=17 without you understanding why. Yes, happened to me. – RaphaMex Dec 17 '17 at 03:29
  • Might also want to comment on the case where `N` is an object of a class type (which supports the appropriate operations). – Peter Dec 17 '17 at 04:34