سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top