Is there a performance difference between i++
and ++i
if the resulting value is not used?
I always prefer pre-increment, however ...
I wanted to point out that even in the case of calling the operator++ function, the compiler will be able to optimize away the temporary if the function gets inlined. Since the operator++ is usually short and often implemented in the header, it is likely to get inlined.
So, for practical purposes, there likely isn't much of a difference between the performance of the two forms. However, I always prefer pre-increment since it seems better to directly express what I"m trying to say, rather than relying on the optimizer to figure it out.
Also, giving the optmizer less to do likely means the compiler runs faster.
Please don't let the question of "which one is faster" be the deciding factor of which to use. Chances are you're never going to care that much, and besides, programmer reading time is far more expensive than machine time.
Use whichever makes most sense to the human reading the code.
In C, the compiler can generally optimize them to be the same if the result is unused.
However, in C++ if using other types that provide their own ++ operators, the prefix version is likely to be faster than the postfix version. So, if you don't need the postfix semantics, it is better to use the prefix operator.
First of all: The difference between i++
and ++i
is neglegible in C.
To the details.
++i
is fasterIn C++, ++i
is more efficient iff i
is some kind of an object with an overloaded increment operator.
Why?
In ++i
, the object is first incremented, and can subsequently passed as a const reference to any other function. This is not possible if the expression is foo(i++)
because now the increment needs to be done before foo()
is called, but the old value needs to be passed to foo()
. Consequently, the compiler is forced to make a copy of i
before it executes the increment operator on the original. The additional constructor/destructor calls are the bad part.
As noted above, this does not apply to fundamental types.
i++
may be fasterIf no constructor/destructor needs to be called, which is always the case in C, ++i
and i++
should be equally fast, right? No. They are virtually equally fast, but there may be small differences, which most other answerers got the wrong way around.
How can i++
be faster?
The point is data dependencies. If the value needs to be loaded from memory, two subsequent operations need to be done with it, incrementing it, and using it. With ++i
, the incrementation needs to be done before the value can be used. With i++
, the use does not depend on the increment, and the CPU may perform the use operation in parallel to the increment operation. The difference is at most one CPU cycle, so it is really neglegible, but it is there. And it is the other way round then many would expect.
A better answer is that ++i
will sometimes be faster but never slower.
Everyone seems to be assuming that i
is a regular built-in type such as int
. In this case there will be no measurable difference.
However if i
is complex type then you may well find a measurable difference. For i++
you must make a copy of your class before incrementing it. Depending on what's involved in a copy it could indeed be slower since with ++it
you can just return the final value.
Foo Foo::operator++()
{
Foo oldFoo = *this; // copy existing value - could be slow
// yadda yadda, do increment
return oldFoo;
}
Another difference is that with ++i
you have the option of returning a reference instead of a value. Again, depending on what's involved in making a copy of your object this could be slower.
A real-world example of where this can occur would be the use of iterators. Copying an iterator is unlikely to be a bottle-neck in your application, but it's still good practice to get into the habit of using ++i
instead of i++
where the outcome is not affected.
My C is a little rusty, so I apologize in advance. Speedwise, I can understand the results. But, I am confused as to how both files came out to the same MD5 hash. Maybe a for loop runs the same, but wouldn't the following 2 lines of code generate different assembly?
myArray[i++] = "hello";
vs
myArray[++i] = "hello";
The first one writes the value to the array, then increments i. The second increments i then writes to the array. I'm no assembly expert, but I just don't see how the same executable would be generated by these 2 different lines of code.
Just my two cents.