Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x=x+1 vs. x +=1

I'm under the impression that these two commands result in the same end, namely incrementing X by 1 but that the latter is probably more efficient.

If this is not correct, please explain the diff.

If it is correct, why should the latter be more efficient? Shouldn't they both compile to the same IL?

Thanks.

like image 583
Chad Avatar asked Apr 30 '09 17:04

Chad


People also ask

What is the difference between x += 1 and x =+ 1?

Difference between x++ and x=x+1 in Java. In x++, it increase the value of x by 1 and in x=x+1 it also increase the value of x by 1. But the question is that both are same or there is any difference between them.

Is X ++ faster than x += 1?

++, += or x + 1? The bottom line is that in most but not all languages the compiler is going to make them identical anyway, so there's no difference in efficiency.

What is the difference between += and =+?

+ is an arithmetic operator while += is an assignment operator.. When += is used, the value on the RHS will be added to the variable on the LHS and the resultant value will be assigned as the new value of the LHS..

What is difference between x ++ and X 1?

x++ is a const expression that modifies the value of x (It increases it by 1 ). If you reference x++ , the expression will return the value of x before it is incremented. The expression ++x will return the value of x after it is incremented. x + 1 however, is an expression that represents the value of x + 1 .


2 Answers

From the MSDN library for +=:

Using this operator is almost the same as specifying result = result + expression, except that result is only evaluated once.

So they are not identical and that is why x += 1 will be more efficient.

Update: I just noticed that my MSDN Library link was to the JScript page instead of the VB page, which does not contain the same quote.

Therefore upon further research and testing, that answer does not apply to VB.NET. I was wrong. Here is a sample console app:

Module Module1  Sub Main()     Dim x = 0     Console.WriteLine(PlusEqual1(x))     Console.WriteLine(Add1(x))     Console.WriteLine(PlusEqual2(x))     Console.WriteLine(Add2(x))     Console.ReadLine() End Sub  Public Function PlusEqual1(ByVal x As Integer) As Integer     x += 1     Return x End Function  Public Function Add1(ByVal x As Integer) As Integer     x = x + 1     Return x End Function  Public Function PlusEqual2(ByVal x As Integer) As Integer     x += 2     Return x End Function  Public Function Add2(ByVal x As Integer) As Integer     x = x + 2     Return x End Function  End Module 

IL for both PlusEqual1 and Add1 are indeed identical:

.method public static int32 Add1(int32 x) cil managed { .maxstack 2 .locals init (     [0] int32 Add1) L_0000: nop  L_0001: ldarg.0  L_0002: ldc.i4.1  L_0003: add.ovf  L_0004: starg.s x L_0006: ldarg.0  L_0007: stloc.0  L_0008: br.s L_000a L_000a: ldloc.0  L_000b: ret  } 

The IL for PlusEqual2 and Add2 are nearly identical to that as well:

.method public static int32 Add2(int32 x) cil managed {  .maxstack 2 .locals init (     [0] int32 Add2) L_0000: nop  L_0001: ldarg.0  L_0002: ldc.i4.2  L_0003: add.ovf  L_0004: starg.s x L_0006: ldarg.0  L_0007: stloc.0  L_0008: br.s L_000a L_000a: ldloc.0  L_000b: ret  } 
like image 192
CoderDennis Avatar answered Sep 30 '22 18:09

CoderDennis


I wrote a simple console app:

static void Main(string[] args) {     int i = 0;     i += 1;     i = i + 1;     Console.WriteLine(i); } 

I disassembled it using Reflector and here's what i got:

private static void Main(string[] args) {     int i = 0;     i++;     i++;     Console.WriteLine(i); } 

They are the same.

like image 39
Vinicius Rocha Avatar answered Sep 30 '22 20:09

Vinicius Rocha