其实这个问题能够从三个角度去分析:语言规范,编译器实现,CPU支持。首先从语言规范上来说;前置++和后置++是不等价的,前置++在规范中明确指出 和+=组合操做符是等价的,但和E = E+1;这样的赋值操做不等价,由于后者对操做数E须要进行两次求值,而+=组合操做符只进行一次求值。后置++表示操做数做为结果值被获取以后操做数的 原值再进行更新。 聪明的编译器能够根据应用场景进行优化(标准不规定相关优化的手段,由编译器自决),但不能过分依赖这种优化,由于编译器仍是不如人聪明,并且复杂的表达式也不必定能够优化掉。从 CPU的角度上来说CPU支持什么样的指令集是绝对决定了相应操做符计算效率。在嵌入式开发中不一样的CPU之间的差别就大了。好比说PowerPC CPU就没有直接能够对应后缀++的t自增指令,某些版本的ARM CPU没有任何自增指令(不管是前置仍是后置式的)。所以不管从CPU支持来说仍是编译器优化来看前置++确定是高效的,后置++的即时CPU和编译器优化都支持也不能比前置++更高效,在使用的时候应该尽可能使用前置++。C/C++提供这么多操做符是由于它是最接近底层的高级语言,它须要尽量实现CPU对应 指令的高级实现进而为性能优化提供选择。而其它多数高级语言不会给你选择的权利。express
下面是标准中的相关信息:性能优化
The value of the operand of the prefix ++ operator is incremented. The result is the new
value of the operand after incrementation. The expression ++E is equivalent to (E+=1).
See the discussions of additive operators and compound assignment for information on
constraints, types, side effects, and conversions and the effects of operations on pointers.
The result of the postfix ++ operator is the value of the operand. After the result is
obtained, the value of the operand is incremented. (That is, the value 1 of the appropriate
type is added to it.) See the discussions of additive operators and compound assignment
for information on constraints, types, and conversions and the effects of operations on
pointers. The side effect of updating the stored
A compound assignment of the form E1 op= E2 differs from the simple assignment
expression E1 = E1 op (E2) only in that the lvalue E1 is evaluated only once.app