Question

I am confused whether or not it is advisable to use x += 1 or x = x+1. I know that they both produce the same results. In practical terms, are there any performance gain when using x+=1 instead of x = x+1? Will it make my program run faster?

Était-ce utile?

La solution

x += 1 is just syntactical short cut for x = x + 1. AFAIK, No machine (CPU) level instruction set has an instruction to do x += 1 in a single atomic operation. The actual code executed by the CPU should be identical.

Autres conseils

Any good compiler should give you the same machine code for both expressions.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top