r/learnjava • • 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.

https://pastebin.com/F1v0Herr

EDIT2: parallel version shows that preincrement is slightly faster

https://pastebin.com/3gDMWxVL

3 Upvotes

15 comments sorted by

2

u/thisisjustascreename Sep 09 '21

It shouldn't be, can you share your code?

0

u/bitemyshinymetalass0 Sep 09 '21

https://pastebin.com/F1v0Herr

Here it is. I had to use -Xmx tag when I run it at Integer.MAX_VALUE times.

-1

u/bitemyshinymetalass0 Sep 09 '21

nvm. Preincrement is faster when I run it in parallel/concurrent.

https://pastebin.com/3gDMWxVL

4

u/thisisjustascreename Sep 09 '21 edited Sep 09 '21

So, hot-loop microbenchmarks are notoriously unreliable if you measure two things in the same application run. You should really measure pre-increment and post-increment with different compilations, and ideally average several runs.

If I remove the second loop, I get nearly the same value whether I use pre or post increment.

1

u/bitemyshinymetalass0 Sep 09 '21

Ahh good point. Just tried that and just like you said. That's fascinating. When I tried the 2 loops in parallel in one compilation it also displayed very unstable behaviors. Just back to the basics though, how is pre-increment implemented internally? I know post-incre uses a temp value, but I don't know how pre-incre works.

1

u/thisisjustascreename Sep 09 '21

On my machine they generate identical bytecode. It might be slightly different if you use the value of a or b in the same statement. You should check the output of javap -c IncrementTest.class to see.

1

u/jacob_scooter Sep 10 '21

x++ adds one to x after the current operation

++x adds one before the current operation

there shouldn’t be a speed difference

1

u/fredoverflow Sep 11 '21

If you ignore their result, ++x; and x++; compile to the exact same Java bytecode iinc x, 1: https://www.youtube.com/watch?v=bONciSOJ_N4&t=95

1

u/cal-cheese Sep 11 '21

If you want to do microbenchmark, use JMH.

-1

u/[deleted] Sep 09 '21

Well. That's not the point..try doing this with both pre and both post.

0

u/bitemyshinymetalass0 Sep 09 '21

What do you mean both pre and both post? My goal was to compare speed between pre and post.

-4

u/[deleted] Sep 09 '21

Compare speed between both pre and post

1

u/bitemyshinymetalass0 Sep 09 '21

You mean run them in parallel? I just tried and pre is faster as expected. So I guess maybe there's some runtime or compiler optimization happening since the loops are the same.

-10

u/[deleted] Sep 09 '21

Omg just do both pre or both post. a++ with b++ or ++a and ++b

3

u/bitemyshinymetalass0 Sep 09 '21

Wait what. Maybe I'm dumb beyond saving but what's the point in that? I don't need to run it twice for each type of syntax I'm comparing.