Is ++x more efficient than x++ in Java?
Asked Answered
C

3

17

During a programming class, the professor was teaching us about x++ and ++x, with x being an integer.

He said that in the scenario we are able to just put either x++ or ++x, ++x is more efficient (by little, but still, in theory, more efficient nonetheless).

But I forgot why. Anyone knows? This was with Java.

Coxalgia answered 12/9, 2012 at 22:1 Comment(8)
Why would your professor even both with such level of insignificant detail?Aminaamine
Who is this professor? I'm curious. :PPlotinus
Here's the relevant discussion about C++: Preincrement faster than postincrement in C++ - true? If yes, why is it?Pangermanism
Ask the professor for a benchmark in Java to verify this statement :) I also changed the title from an assertion to a question.Memento
In any case, all performance questions that ask "why" should come with a benchmark and an SSCCE.Plotinus
Well, as a minimum, he said it was better :(Coxalgia
@Omega "Better" generally needs qualification to avoid being considered entirely subjective ;-) In any case, I prefer x++ and I avoid using ++ or -- in expressions. That is, I only use the statement x++; (except when part of a for-loops iterator, where it might be for(..; ..; x++, y++), etc).Memento
My CS102 professor explained the difference between ++x and x++ (we were learning C) for the sake of saying "This is stupid, do not argue over this ever again. That is the only reason we are covering it." Which seems like a good reason to bring it up.Sennit
D
27

It's not more efficient in Java. It can be more efficient in languages where the increment/decrement operators can be overloaded, but otherwise the performance is exactly the same.

The difference between x++ and ++x is that x++ returns the value of x before it was incremented, and ++x returns the value of x after it was incremented. In terms of code generation, both make up for the exact same number of instructions, at least when you can use either interchangeably (if you can't use them interchangeably, you shouldn't be worrying about which one is faster, you should be picking the one you need). The only difference is where the increment instruction is placed.

In C++, classes can overload both the prefix (++x) and postfix (x++) operators. When dealing with types that overload them, it is almost universally faster to use the prefix operator because the semantics of the postfix operator will return a copy of the object as it was before the increment, even when you wouldn't use it, while the prefix operator can simply return a reference to the modified object (and God knows C++ developers prefer to return references rather than copies). This could be a reason to consider ++x superior to x++: if you gain the habit of using ++x you could save yourself some slight performance trouble when/if you switch to C++. But in the context of Java only, both are absolutely equivalent.

Much like pst in the comments above, I never use the return value of x++ or ++x, and if you never do either, you should probably just stick to the one you prefer.

Doorstop answered 12/9, 2012 at 22:2 Comment(1)
"C++ developers prefer to return references rather than copies"... because copies are expensive? Sure, compilers can eliminate copies in many practical situations, but why rely on that assumption when you can avoid creating one in the first place?Tennessee
D
16

Out of curiosity, you can check the generated bytecode.

This program:

public static void main(String args[]) {
    int i = 1; //bytecode 0, 1
    i++; //bytecode 2
    ++i; //bytecode 5
    int a = ++i; //bytecode 8, 11, 12
    int b = i++; //bytecode 13, 14, 17
}

generates the following bytecode:

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1
       2: iinc          1, 1
       5: iinc          1, 1
       8: iinc          1, 1
      11: iload_1
      12: istore_2
      13: iload_1
      14: iinc          1, 1
      17: istore_3
      18: return

So you can see that pre and post fix operators are strictly identical from a bytecode perspective (apart from the order of the operations).

If and when the JIT compiles that part of your code, all bets are off. For example, the code above, as is, could be compiled as a no-op because it has no side effects.

Doubletalk answered 12/9, 2012 at 22:46 Comment(0)
D
4

The truth of the matter is that *x++ was faster than *++x in C on a PDP-11, and maybe a VAX, because it compiled to a single instruction (autoincrement register deferred). Similarly *--x was faster than *x--. Using either form without the dereference would only be faster in one case rather than the other if the compiler still generated that instruction, which would have been wasteful because the dereference would still have occurred.

Those days are long gone.

Defraud answered 12/9, 2012 at 23:29 Comment(1)
I'm pretty sure this is actually wrong, and on the PDP-11 it's both *x++ and *x-- that were more efficient than the other. Now, in a modern architecture, and not referring to dereferencing, x++ may require a temporary that ++x doesn't, and this is probably what the people telling the OP meant.Epiphenomenalism

© 2022 - 2024 — McMap. All rights reserved.