Avatar ·

Is the < operator faster than <=? Performance comparison in C++

Hi everyone! I'm a beginner C++ developer, and I have a question about the performance of comparison operators.\n\nI'm writing a loop that iterates through an array of 10 million elements, and I need to perform some action while the index is less than or equal to a certain value. I'm using `for (int i = 0; i <= n; i++)`. But my colleague said it's better to use `i < n + 1`, because the `<` operator is faster than `<=`. He claims that at the assembly level, `<` executes in one CPU cycle, while `<=` takes two cycles because the processor needs to additionally check for equality.\n\nI tried both options in my code:\n\n```cpp\n#include \n#include \n\nconst int N = 10000000;\n\nvoid test_less_equal() {\n int sum = 0;\n for (int i = 0; i <= N; ++i) {