August 1, 2020

i++ or ++i banner

i++ or ++i? Which is the more efficient? Let me tell you a short story.
A few years ago, a colleague caught me using i++ in a "for loop" during a code review. He lectured me about why the usage of i++ is not correct for a performance matter. Unfortunately, his explanation was incomplete.
Since this day, I am using ++i. For performance matter? No, principally to avoid a long explanation about why I was using i++.
Today, I realized that I never checked why some developers prefer one or the other version. Let's check together!

When to use the "i++" or "++i"?

Some people could say that the difference is not important. However, the usage of i++ and ++i is so common that the knowledge of their difference is primordial. The devil is in the details. This kind of code reveals perfectly the difference:

#include <stdio.h>

int main()
{
    int i = 0;
    int j = 0;
    printf("FIRST: i = 0, with ++i ==> %d\n", ++i);
    printf("SECOND: j = 0, with j++ ==> %d\n", j++);
    printf("THIRD: i = %d, j = %d\n", i, j);
    return 0;
}

Now, let's build and execute this code:

FIRST: i = 0, with ++i ==> 1
SECOND: j = 0, with j++ ==> 0
THIRD: i = 1, j = 1
  • ++i : Increment the value and returns the incremented value.
    → Increment before the function call/operation.
  • i++ : Increment the value but returns the original value (before being incremented)
    → Increment after the function call/operation.

What about the performances?

After some tentative, conversion to assembly code, etc. Short Answer: The difference is minimal. In the majority, the compiler optimizes your code.

Performances concerning the speed

++i is always faster than i++. This kind of code shows the difference:

#include <stdio.h>

int main()
{
    int i = 0;
    int j = ++i;
    return 0;
}
#include <stdio.h>

int main()
{
    int i = 0;
    int j = i++;
    return 0;
}

And now, let's see the assembly code (GCC 8.3) - On the right, ++i. On the left, i++ (Usage of Compiler Explorer):

++i case:

i++ case:

There is only one instruction difference between ++i and i++.
Indeed, in the ++i case, we see that the variable "i" is first incremented before the assignment to a new variable - Pretty simple!
Concerning the i++ case, the operation is a bit more complex:

  • First, "mov" to save the data variable in another registry. - eax = 0
  • Second, the instruction "lea" load a new "edx" value incremented by one. - edx = 1
  • Third, "mov", "edx" is copied in the variable "i". - i = edx →i = 1
  • Last, "mov", the saved value "eax" (equal to 0) is copied in "j". j = eax → j = 0
When ++i and i++ have the same speed?

During a simple operation (without other variable implied or functions), I get the same performances:

#include <stdio.h>

int main()
{
    int i = 0;
    for(; i < 10; ++i)
    {
        printf("i = %d\n", i);
    }
    return 0;
}
#include <stdio.h>

int main()
{
    int i = 0;
    for(; i < 10; i++)
    {
        printf("i = %d\n", i);
    }
    return 0;
}

Here you know the routine! Let's check the assembly code!

++i case:

i++ case:

The output is the same... There are no differences in the performances.

What about the compiler used?

The usage of ++i is recommended.
As we saw, the difference is minimal and is applicable during function calls or "complex" operations. i.e, when the variable value should be saved with i++.

Another point concerning this post: The performances between ++i and i++ is highly dependent on your compiler.
Re-test those codes with a MSVC compiler; you'll see a result entirely different!

++i case:

i++ case:

Summarize - So, i++ or ++i?

Now, you know everything about ++i and i++. I want to highlight those points:

  • ++i is always faster than i++.
  • If there are no reasons to use i++, The recommendation is to use ++i.
  • In for loops, no performance differences.
  • No function call, simple operation. The compiler will optimize.
  • Highly dependent on the compiler
  • The performance difference is not apparent and not always true
  • The performance depends on the function used, operation and compiler.

About the author 

Axel Fortun

​Developer specialized in Linux environment and embedded systems.
​Knowledge in multiple languages as C/C++, Java, Python​ and AngularJs.
​Working as Software developer since 2014 with a beginning in the car industry​ and then in ​medical systems.