可能重複:
Is there a performance difference between i++ and ++i in C++?運算符x ++;和++ x;對於int.哪個更快?爲什麼?
他們說,++i
是快,但我不明白why.Can任何人告訴我這些運營商的彙編代碼?
可能重複:
Is there a performance difference between i++ and ++i in C++?運算符x ++;和++ x;對於int.哪個更快?爲什麼?
他們說,++i
是快,但我不明白why.Can任何人告訴我這些運營商的彙編代碼?
++i
確實如i++
一樣快,但它可能會更快。
原因是執行。
爲了實現i++
,實現需要生成i
的臨時副本,與++i
的實現不同。
但智能編譯器可以優化這種臨時性的生成性,它們當然適用於POD類型。
這取決於編譯器和情況,如果它爲此表達式生成更快的代碼。
請參閱這裏:http://stackoverflow.com/questions/3346450/c-what-is-the-difference-between-i-and-i/3346729#3346729 – Azodious
@Abodious:C#!= C++ – Piskvor
這有之前被問過很多次了,除了提及引用外,還有http://stackoverflow.com/questions/2020184/preincrement-faster-than-postincrement-in-c-true-if-yes-why-is-it和http ://stackoverflow.com/questions/5223950/stl-iterators-prefix-increment-faster。 – hlovdal