0
int i = 0;
i += 1;
i = i + 1;

What is the difference between these 2 options? What changes in performance time terms? Which is the most powerful?

Hoffman
  • 72
  • 1
  • 9
  • 5
    http://stackoverflow.com/questions/29179528/i-i-i1-and-i-1-which-one-is-faster – Sahi Apr 08 '16 at 19:55
  • Obligatory: the choice between `i += 1` and `i = i + 1` will have absolutely no discernible impact on a program's performance. Programs are slow because they do things like create millions of objects, do incredibly complicated computations or wait on server requests. – Radiodef Apr 08 '16 at 20:25

4 Answers4

6

+= does implicit casting. For example this will compile:

int i = 0;
i += 1L;

And this will not:

int i = 0;
i = i + 1L;

Tried to compile both snippets with my jdk1.8.0_11 on Windows 8 and see bytecode difference...

0: iconst_0
1: istore_1
2: iinc          1, 1

for i += 1 version, and:

0: iconst_0
1: istore_1
2: iload_1
3: iconst_1
4: iadd
5: istore_1

for i = i + 1 version.

So the conclusion is: you may indeed get different bytecode (or may not, see @TDG answer) and different performance, but the difference is insignificant compared to other overheads your programs will have.

Yaroslav Rudykh
  • 803
  • 6
  • 12
2

You have to think in terms of the assembly code generated and what assembly code generated is completely dependent on what compiler is being used. Some compilers will make these differences non existent as they will performance tune your statements. However in general...

i += 1;

Is slightly more efficient than..

i = i + 1;

because the address of "i" is only accessed once in "i += 1". It saves you one assembly operation which is usually not a big deal unless your computation might be done through many iterations. It results in saving you an assembly "mov" instruction.

Art Solano
  • 99
  • 11
  • 1
    is this true ? do processors have an operation "+=" ? – Vic Seedoubleyew Apr 08 '16 at 19:51
  • 2
    Did you actually checked the Byte Code of these two lines and saw any difference? – TDG Apr 08 '16 at 19:54
  • I agree. I think even if processors do have an operation "+=", then compilers would probably detect "i=i+1" and would use the "+=" operation – Vic Seedoubleyew Apr 08 '16 at 19:56
  • In assembly you put data into registers, then perform an operation. rather than doing two "mov" operations, you only need one. Then you perform an operation on it. And yes some assemblies support "inc", which will increment what ever is in a register by one. – Art Solano Apr 08 '16 at 19:57
  • There might be intelligent compilers that will take "i = i + 1" and convert it to the same assembly that is "i += 1", or better yet "i++" – Art Solano Apr 08 '16 at 19:59
1

The Byte Code of the next two "programs":

//first "program"
int i = 0;
i = i + 1;

//second program
int i = 0;
i += 1;

is the same:

0: iconst_0
1: istore_1
2: iinc          1, 1
5: return

And when decompiling the above Byte Code we get this -

int i = 0;
++i;

so it does not matter which one you use.

EDIT The above was tested on jdk1.7.0_79 and Eclipse 3.8.2.

TDG
  • 5,909
  • 3
  • 30
  • 51
0

i=i+1; it will have to load the value of i, add one to it, and then store the result back to i

i+=1; add one to it, and then store the result

EDIT 1: some people say that the second (i+=1) is more faster, but if you make disassembly this part code in see C++(vs 2013), you can see that they are the same. Unfortunately, I don not know how to make this in JAVA.

    i = i + 1;
00AF52C5  mov         eax,dword ptr [i]  
00AF52C8  add         eax,1  
00AF52CB  mov         dword ptr [i],eax  
    i +=1;
00AF52CE  mov         eax,dword ptr [i]  
00AF52D1  add         eax,1  
00AF52D4  mov         dword ptr [i],eax  
Bushuev
  • 557
  • 1
  • 10
  • 29
  • well this is true only if "add one to it" is one less operation. What makes you think it is ? and actually, even if it is, then compilers would probably detect "i=i+1" and would use the "+=" operation – Vic Seedoubleyew Apr 08 '16 at 19:52
  • @TDG you are right – Bushuev Apr 08 '16 at 20:05
  • @VicSeedoubleyew I am not know how to see inner code in JAVA, but I know how to do it in C#(inner code IL) and C++(assembler) – Bushuev Apr 08 '16 at 20:08