为x + = 1比X = X + 1更有效? [英] Is x += 1 more efficient than x = x + 1?
问题描述
在 X = X + 1
是 X
两次评估?如果是这样,在平均值x + = 1
, X
只计算一次?如何在两个前pressions在编译器中间code进行评价?
In x = x + 1
, is x
evaluated twice? If so, does that mean in x += 1
, x
is only evaluated once? How are the two expressions evaluated in terms of compiler intermediate code?
例如, X ++
可能意味着:以的位置x
,加载<$ C $的内容C> X 到寄存器,并在内存中增加x 的值。
For example, x++
could mean : take the location of x
, load the contents of x
into a register, and increment the value of x
in memory.
此外,我已经读了 X + = 1
是有用的,当 X
不是一个简单的变量,而是一个前pression涉及的阵列。任何想法,为什么出现这种情况?
Also I have read that x += 1
is useful when x
is not a simple variable, but an expression involving an array. Any ideas why this is the case?
推荐答案
在大多数编译器这些将是相同的。
In most compilers these would be identical
这篇关于为x + = 1比X = X + 1更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!