2011-11-10 28 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

這取決於編譯器和情況,如果它爲此表達式生成更快的代碼。

相關問題