0

Is it more performant to do a pre-increment vs a post-increment in a for loop in java ?

Sample code :

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

and

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

I notice that when i do a pre-increment, the execution time is lesser that when i do the post-increment. Any suggestions on why this might be the case ? thanks.

lokoko
  • 5,785
  • 5
  • 35
  • 68
  • What was your test case where the time was less? – Danny Feb 27 '13 at 15:12
  • 10
    show your benchmark. There should be no difference.... – jlordo Feb 27 '13 at 15:12
  • could because of the fact that post increment requires extra copy. :). – Adnan Akbar Feb 27 '13 at 15:14
  • I will agree that this is not possible. I tried myself and see no difference in runtimes. But it sounds very interesting. Could you post your test cases and your benchmarks? – nikkatsa Feb 27 '13 at 15:14
  • If your benchmark consists in measuring the time for 10 iterations, the precision of the internal clock is probably less than the time of the whole operation you are trying to measure. – assylias Feb 27 '13 at 15:17
  • I don't see how it takes less time considering its only a loop from 0 to 9 in the first case and 1 to 9 in the second case. `++i` increments before the block of code is run and `i++` increments after the block of code is run. – pattmorter Feb 27 '13 at 15:19
  • 1
    @pattmorter Both `++i` and `i++` result in the exact same observed behavior. Your statement is nonsense. – Marko Topolnik Feb 27 '13 at 15:20
  • 1
    @pattmorter huh... no, it increments from 0 to 10 in both cases, then exits the loop because 10 >= 10. – assylias Feb 27 '13 at 15:20
  • @pattmorter: **No**, wrong. Read the Explanation in this answer: [get multiple random values from ArrayList HashMap](http://stackoverflow.com/questions/14914062/get-multiple-random-values-from-arraylist-hashmap/14914068#14914068) – jlordo Feb 27 '13 at 15:24
  • I did mess up the `++i` thing. But What i meant in my orginal comment is if you output the i itll only output 0 to 9. Sorry if you misunderstood me. – pattmorter Feb 27 '13 at 15:38
  • Isnt it the case that in i++ it tries to create a temporary variable to store the value and then increment and hence might take more time ? – lokoko Feb 27 '13 at 16:56
  • Would be pretty helpful, if the already answered questions gets linkt in the "duplicate"-statement – TheTrowser May 15 '15 at 13:29

4 Answers4

10

I compiled a minimal example (Oracle jdk1.7.0_07):

public void post() {
    for (int i=0; i<10; i++) {

    }
}

public void pre() {
    for (int i=0; i<10; ++i) {

    }
}

Both methods produced the same exact bytecode:

 0 iconst_0
 1 istore_1
 2 goto 8 (+6)
 5 iinc 1 by 1
 8 iload_1
 9 bipush 10
11 if_icmplt 5 (-6)
14 return
jlordo
  • 37,490
  • 6
  • 58
  • 83
  • Of course, the JIT will compile this whole shebang into a single `return` instruction. – Marko Topolnik Feb 27 '13 at 15:21
  • @MarkoTopolnik: Will JIT always do it, or just for a large number of iterations? – jlordo Feb 27 '13 at 15:24
  • 1
    Whenever the JIT compiles it, you can be quite certain that it will do it (I've tested this several times). Of course, the JIT itself will by default not bother with code that isn't being executed a lot. – Marko Topolnik Feb 27 '13 at 15:26
3

Unless your loop body does next to nothing, this question is moot: the overhead of variable increment is zero. Even if the overhead stands out, its magnitude is completely unpredictable and can vary wildly even across different instantiations of the exact same JVM on the exact same hardware. The "wild" variation is, of course, within the range of 0..2 ns.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Post-increment and pre-increment (or decrement) basically means if you are using pre-increment such as ++i this is evaluated before the rest of that code line is. If using post-increment such as i++ this is evaluated after the rest of the code line is. That is maybe why you're getting a "faster execution time".

Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51
  • 4
    whether it executes first or last does not affect total performance. – 75inchpianist Feb 27 '13 at 15:15
  • you're right, it does not affect the total performance. But I just wanted to explain why could he be feeling a faster execution. But agree with you about the total performance statement. – Marcelo Tataje Feb 27 '13 at 15:19
0

There is no such performance issue based on that. Both are exact same when it comes to performance. And there is no way you can claim ++i should use instead of i++, it is up to the logic we are going to handle. ++i means increment first then go to work while i++ means go to work and then increment. Have a look at the following questions.

How do the post increment (i++) and pre increment (++i) operators work in Java?

Language Independent example: Difference between i++ and ++i in a loop?

Community
  • 1
  • 1
PeakGen
  • 21,894
  • 86
  • 261
  • 463