<分区>
Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?
对于以下的使用,请以C语言的执行时间来评价。 在一些采访中,我被问到我应该使用这些变体中的哪个以及为什么。
a++
++a
a=a+1
a+=1
<分区>
Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?
对于以下的使用,请以C语言的执行时间来评价。 在一些采访中,我被问到我应该使用这些变体中的哪个以及为什么。
a++
++a
a=a+1
a+=1
最佳答案
这是 g++ -S
产生的结果:
void irrelevant_low_level_worries()
{
int a = 0;
// movl $0, -4(%ebp)
a++;
// incl -4(%ebp)
++a;
// incl -4(%ebp)
a = a + 1;
// incl -4(%ebp)
a += 1;
// incl -4(%ebp)
}
因此,即使没有任何优化器开关,所有四个语句都会编译为完全相同的机器代码。
关于c++ - rate++a,a++,a=a+1 和 a+=1 在 C 中的执行效率。假设 gcc 是编译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3557489/