Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ++x more efficient than x++ in Java?

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.

like image 319
Voldemort Avatar asked Sep 12 '12 22:09

Voldemort


People also ask

Which is faster x ++ or ++ x?

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).

Is x ++ or x 1 more efficient?

This is the only way in which x+=1 is "more efficient" than x=x+1 . However the += operator does have a few other advantages, like the fact that (x)+=1 works in macros where x is an expression that may have side effects which you want to avoid evaluating more than once...

How does X differ from X?

'x' indicates that this value is a single character and can be stored by a variable having the data type as char. Whereas “x” indicates that even though it's a single character but it is going to be stored in the variable having data type as String.

What is the difference between -- x and x --?

it's simple, --x will be decremented first and then gets evaluated. whereas the x-- will be evaluated first and then gets decremented. for example, int a =5, b =5; int c= --a; // here, c =4 and a=4 after evaluation. //why?


2 Answers

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.

like image 143
zneak Avatar answered Sep 22 '22 11:09

zneak


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.

like image 34
assylias Avatar answered Sep 19 '22 11:09

assylias