post and pre increment impact on performance...

I wondering if there are any advantages of preincrementing in java like in a for like:

for (int i=0; i<aBigNumber; i++)

for (int i=0; i><aBigNumber; ++i)

Would ++i be faster? i++? or same?>

[220 byte] By [rugaea] at [2007-11-27 11:59:59]
# 1

they have the same performance.

you can test this using System.currentTimeMillis()

java_2006a at 2007-7-29 19:28:46 > top of Java-index,Java Essentials,Java Programming...
# 2

I believe ++i could be a teeny tiny bit faster because you don't have to temporarily store the current alue of i.

Good luck creating realistic code where the difference is noticeable though. You don't pick ++i vs. i++ for performance reasons. You pick the one whose semantics best fits your needs.

jverda at 2007-7-29 19:28:46 > top of Java-index,Java Essentials,Java Programming...
# 3

Any compiler worth its price will optimize ++i and i++ to the same code when the result is not used.

javac, for example, produces identical bytecode for both.

sjasjaa at 2007-7-29 19:28:46 > top of Java-index,Java Essentials,Java Programming...
# 4

If you are worried about performance then don't increase i by 1, use 10, and it will be 10 times faster.

camickra at 2007-7-29 19:28:46 > top of Java-index,Java Essentials,Java Programming...
# 5

> If you are worried about performance then don't

> increase i by 1, use 10, and it will be 10 times

> faster.

I have an routine that increments by "11". It's one faster, isn't it? It's not ten. You see, most blokes, you know, will be incrementing by ten. You're on ten here, all the way up, all the way up, all the way up, you're on ten in your program. Where can you go from there? Where?

petes1234a at 2007-7-29 19:28:46 > top of Java-index,Java Essentials,Java Programming...
# 6

for(int i=0;i<Integer.MAX_VALUE;i = i+500000000)

>

cotton.ma at 2007-7-29 19:28:46 > top of Java-index,Java Essentials,Java Programming...
# 7

I don't remember amps going up to 500000000 in "This is Spinal Tap"

petes1234a at 2007-7-29 19:28:46 > top of Java-index,Java Essentials,Java Programming...
# 8

for(int i=0; i <= Integer.MAX_VALUE; i += Integer.MAX_VALUE)

What do I win?

CaptainMorgan08a at 2007-7-29 19:28:46 > top of Java-index,Java Essentials,Java Programming...