Is the < comparison operator faster than <=? Does operator choice affect performance in C++?
Hi everyone! I'm a beginner C++ developer, and I've been bothered by one question for several days. I write loops with conditions, and I often wonder: is there a performance difference between `i < n` and `i <= n`? For example, at the assembly level, these operations might execute differently. I've heard that on older processors, "less than" comparison could be faster than "less than or equal to" because the latter requires an additional equality check. But now, in 2024, is this still relevant?
Here's a simple code example that concerns me:
```cpp
int sum = 0;
for (int i = 0; i < 1000000; ++i) {
sum += i;
}
```
vs
```cpp
int sum = 0;
for (int i = 0; i <= 999999; ++i) {
sum += i;
}
```
Both loops do the same thing, but the first uses `<`, while the second uses `<=`. The compiler opt
No answers yet.
Log in to leave an answer