2011-11-10 27 views
3

可能重复:
Is there a performance difference between i++ and ++i in C++?运算符x ++;和++ x;对于int.哪个更快?为什么?

他们说,++i是快,但我不明白why.Can任何人告诉我这些运营商的汇编代码?

+0

请参阅这里:http://stackoverflow.com/questions/3346450/c-what-is-the-difference-between-i-and-i/3346729#3346729 – Azodious

+0

@Abodious:C#!= C++ – Piskvor

+0

这有之前被问过很多次了,除了提及引用外,还有http://stackoverflow.com/questions/2020184/preincrement-faster-than-postincrement-in-c-tr​​ue-if-yes-why-is-it和http ://stackoverflow.com/questions/5223950/stl-iterators-prefix-increment-faster。 – hlovdal

回答

5

++i确实如i++一样快,但它可能会更快。
原因是执行。

为了实现i++,实现需要生成i的临时副本,与++i的实现不同。

但智能编译器可以优化这种临时性的生成性,它们当然适用于POD类型。

1

这取决于编译器和情况,如果它为此表达式生成更快的代码。

相关问题