Is there a performance difference between using int a = a + 1 and a ++ in java? If so, which is better and why?

Could you briefly explain to me to understand this?

+7
source share
6 answers

Looking at the generated bytecode:

public static void main(String[] args) { int x = 1; int y = 1; int z = 1; int a = 1; int b = 1; x = x + 1; y++; ++z; a += 1; b += 2; } 

generates (use javap -c classname )

 0: iconst_1 1: istore_1 2: iconst_1 3: istore_2 4: iconst_1 5: istore_3 6: iconst_1 7: istore 4 9: iconst_1 10: istore 5 12: iload_1 13: iconst_1 14: iadd 15: istore_1 16: iinc 2, 1 19: iinc 3, 1 22: iinc 4, 1 25: iinc 5, 2 28: return 

So using (jdk1.6.0_18):

 x = x + 1 

creates

 12: iload_1 13: iconst_1 14: iadd 15: istore_1 

then

 y++; ++z; a += 1; 

all result

 iinc 

However, when performing a crude performance test on my laptop as a result, there were no differences in runtime between them (sometimes ++ x was faster, sometimes x = x + 1 was faster), so I would not worry about the performance implications.

+3
source

First of all, the Java language specification says nothing about timing. But assuming we use a typical compiler such as Suns javac, we see that all of the above examples ( a++ , ++a , a += 1 , a = a + 1 ) can be compiled into something like:

  • iinc instruction working with variables:

     iload_<variable> iinc <variable>, 1 istore_<variable> 
  • iadd instack using stack (here variable 1 is used as storage):

     iload_1 iconst_1 iadd istore_1 

It is up to the compiler to choose the best way to compile them. For example. there is no difference between them. And there should be no difference between statements - they all express the same thing: adding one to a number.

Be that as it may, the iinc and iadd can be compiled using JIT for something fast and platform dependent, and in the end I would suggest that the normal runtime compiles both versions into the same assembler code.


With my compiler * jdk1.6.0_20 * the "increment" methods even use the same instruction.

 public class Test { public static void main(String[] args) { int a = 0; a = a + 1; a += 1; a++; ++a; } } 

This is a parsing:

 Compiled from "Test.java" public class Test extends java.lang.Object{ public Test(); Code: 0: aload_0 1: invokespecial #8; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: iconst_0 1: istore_1 2: iinc 1, 1 // a = a + 1; 5: iinc 1, 1 // a += 1; 8: iinc 1, 1 // a++; 11: iinc 1, 1 // ++a; 14: return } 
+16
source

No, there will be no noticeable difference. Use what you find most readable (usually a++ ).

The first rule of code optimization: not.

+2
source

The compiler should optimize and there should be no difference. But keep in mind that the prefix increment operator can be (this depends on the compiler) faster than the postfix equivalent (in C ++ and C # as well):

++a faster than a++ because the postfix statement should create a temporary variable .. think about their implementation:

Prefix:

 a = a + 1; return a; 

postfix:

 int tmp = a; a = a + 1; return tmp; 
+1
source

a ++ is much faster. It is converted to an INC command for assembler. But I think the JVM optimizes a = a + 1, so you don't need to worry about that.

0
source

The same thing, and at present with compiler optimization, you should not be aware of this in order to improve performance, check other larger problems, such as allocs :)

0
source

All Articles