In Java, would there be any performance impact in using post increment vs pre increment operator? (In other languages, a pre-increment can be faster than a post-increment in certain contexts.)
For example, is there a performance difference in these loops?
for (int n = 0; idx < max; ++idx) { /* Note pre-increment */
f(max);
}
Vs.
for (int n = 0; idx < max; idx++) { /* Note post-increment */
f(max);
}
As a result, pre-increment is faster than post-increment because post-increment keeps a copy of the previous value where pre-increment directly adds 1 without copying the previous value.
++i is sometimes faster than, and is never slower than, i++. For intrinsic types like int, it doesn't matter: ++i and i++ are the same speed. For class types like iterators or the previous FAQ's Number class, ++i very well might be faster than i++ since the latter might make a copy of the this object.
What is the Difference Between i++ and ++i in Java? ++i and i++ both increment the value of i by 1 but in a different way. If ++ precedes the variable, it is called pre-increment operator and it comes after a variable, it is called post-increment operator.
Pre-increment and Post-increment Program in C++ returned whereas in the post-increment operator, a local copy of the object is saved, the value attribute of the object is incremented and the reference to the local copy of the object is returned.
The required runtime for the increment itself ought to be the same, but if you are using a pre- or post-increment obviously may have impact on the performance of the surrounding code and a post-increment can in more situations be optimized away.
There are also some non-obvious cases, where the performance of the surrounding code is changed. At least when run with Oracle's server VM on x86 or x64 hardware, the following loops have a significant difference in their runtime:
long a=0, b=0;
for(int i=0;i<1000000;i++) {
b = 3;
a += i * (b++);
}
...
long a=0, b=0;
for(int i=0;i<1000000;i++) {
b = 3;
a += i * (++b);
}
A performance question only makes sense in a context where the functional behavior is identical (since, if the functionality is different, a correct behavior is superior to a minutely-faster one), so I'm assuming that you're referring to a situation where the value of the expression is not used? That is, where the only purpose of the expression is to increment i
? In such a situation, the answer is no: no performance difference, and in fact, no difference whatsoever. I just compiled this class:
public class Foo
{
public static void main(final String args[])
{
int i = Integer.parseInt(args[0]);
i++;
}
}
and computed the MD5 checksum of the resulting Foo.class
; and, similarly for a version with ++i
instead. They had the same checksum, indicating that the two versions were compiled into the exact same bytecode, and would therefore perform literally identically.
(Naturally, this could, in theory, depend on the compiler. A different compiler might decide to compile i++
differently from ++i
even in a context where they're equivalent. But I doubt that, and it's really not worth worrying about even if it the case.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With