简体   繁体   中英

Performance difference between using int a=a+1 and a++ in Java

Is there any performance difference between using int a=a+1 and a++ in Java ?

If so which is better and why? Could you briefly explain me to understand this?

First of all, the Java Language Specification doesn't say anything about timing. But assuming we're using a typical compiler such as Suns javac we see that all of the above examples ( a++ , ++a , a += 1 , a = a + 1 ) could either be compiled into something like:

  • iinc instruction, working on variables:

     iload_<variable> iinc <variable>, 1 istore_<variable>
  • iadd instuction, using the stack (here using variable 1 as a the storage):

     iload_1 iconst_1 iadd istore_1

It's up to the compiler to choose the best possible way to compile them. Eg there is no difference between them. And it shouldn't be any difference between the statements - they all express the same thing - adding one to a number.

That beeing said, both the iinc and the iadd version can be compiled using the JIT to something fast and platform dependent, and in the end I would assume that a normal runtime compiles both versions into the same assembler code.


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

public class Test {
    public static void main(String[] args) {

        int a = 0;

        a = a + 1;
        a += 1;
        a++;
        ++a;
    }
}

This is the disassembly:

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

}

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

whereas

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

all result in

iinc

However, doing a rough performance test on my laptop resulted in next to no difference in the runtime between the two (sometimes ++x was quicker, sometimes x=x+1 was quicker), so I wouldn't worry about the performance implications.

No, there won't be any noticeable difference. Use what you find the most readable (which is a++ , normally).

First rule of code optimization: don't.

The compiler should optimize and there should be no difference at all. But keep in mind that prefix increment operator may be (it depends by the compiler) faster than the postfix equivalent (in C++ and C# also):

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

prefix:

a = a + 1;
return a;

postfix:

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

a++ is much faster. It converts to INC command of assembler.But I think JVM will optimize a=a+1 so you don't need to care about that.

是一样的,现在编译器优化不应该意识到那些东西,为了提高你的性能检查其他更大的问题,比如allocs :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM