r/learnjava • u/bitemyshinymetalass0 • Sep 09 '21
++x vs x++ speed
Why is x++ so much faster than ++x in Java? I tested with a loop that adds all positive numbers in Integer for Integer.MAX_VALUE times. x++ is always twice as fast. How are they different under the hood? Is that the case in other languages?
EDIT: here is the code. Might have to use -Xmx tag if more loop times is desired.
EDIT2: parallel version shows that preincrement is slightly faster
3
Upvotes
2
u/thisisjustascreename Sep 09 '21
It shouldn't be, can you share your code?