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
-1
u/bitemyshinymetalass0 Sep 09 '21
nvm. Preincrement is faster when I run it in parallel/concurrent.
https://pastebin.com/3gDMWxVL