c++ 前自增/后自增操作符效率分析
1、前自增/后自增操作符示例
class Integer { public: // ++i first +1,then return new value Integer &operator++() { value_ += 1; return *this; } // i++ first save old value,then +1,last return old value Integer operator++(int) { Integer old = *this; value_ += 1; return old; } private: int value_; };
2、分別基于內(nèi)置數(shù)據(jù)類型和自定義數(shù)據(jù)類型做測試
#include <iostream> #include <vector> #include <windows.h> int main() { const int sizeInt = 0x00fffffe; const int sizeVec = 0x000ffffe; LARGE_INTEGER frequency; QueryPerformanceFrequency(&frequency); { int* testValue = new int[sizeInt]; LARGE_INTEGER start; LARGE_INTEGER stop; QueryPerformanceCounter(&start); for (int i = 0; i < sizeInt; ++i) { testValue[i]++; } QueryPerformanceCounter(&stop); const auto interval = static_cast<double>(stop.QuadPart - start.QuadPart); const auto timeSpan = interval / frequency.QuadPart * 1000.0; //ms std::cout << "i++ " << sizeInt << " times takes " << timeSpan << "ms." << std::endl; delete[] testValue; } { int* testValue = new int[sizeInt]; LARGE_INTEGER start; LARGE_INTEGER stop; QueryPerformanceCounter(&start); for (int i = 0; i < sizeInt; ++i) { ++testValue[i]; } QueryPerformanceCounter(&stop); const auto interval = static_cast<double>(stop.QuadPart - start.QuadPart); const auto timeSpan = interval / frequency.QuadPart * 1000.0; //ms std::cout << "++i " << sizeInt << " times takes " << timeSpan << "ms." << std::endl; delete[] testValue; } { const std::vector<int> testVec(sizeVec); LARGE_INTEGER start; LARGE_INTEGER stop; QueryPerformanceCounter(&start); for (auto iter = testVec.cbegin(); iter != testVec.cend(); iter++) { } QueryPerformanceCounter(&stop); const auto interval = static_cast<double>(stop.QuadPart - start.QuadPart); const auto timeSpan = interval / frequency.QuadPart * 1000.0; //ms std::cout << "iterator++ " << sizeVec << " times takes " << timeSpan << "ms." << std::endl; } { const std::vector<int> testVec(sizeVec); LARGE_INTEGER start; LARGE_INTEGER stop; QueryPerformanceCounter(&start); for (auto iter = testVec.cbegin(); iter != testVec.cend(); ++iter) { } QueryPerformanceCounter(&stop); const auto interval = static_cast<double>(stop.QuadPart - start.QuadPart); const auto timeSpan = interval / frequency.QuadPart * 1000.0; //ms std::cout << "++iterator " << sizeVec << " times takes " << timeSpan << "ms." << std::endl; } return 0; }
3、五次執(zhí)行結(jié)果
4、結(jié)果分析及結(jié)論
從上面的執(zhí)行結(jié)果可以看出來,對int類型的測試中前自增和后自增耗費(fèi)時(shí)間基本不變;而對std::vector中iterator的測試顯示,前自增所耗費(fèi)的時(shí)間幾乎是后自增的一半。這是因?yàn)椋诤笞栽龅牟僮髦?,會首先生成原始對象的一個(gè)副本,然后將副本中的值加1后返回給調(diào)用者,這樣一來每執(zhí)行一次后自增操作,就會增加一個(gè)對象副本,效率自然降低了。
因此可以得出結(jié)論:對于C++內(nèi)置類型的自增而言,前自增、后自增的效率相差不大;對于自定義類型(類、結(jié)構(gòu)體)的自增操作而言,前自增的效率幾乎比后自增大一倍。
5、注意事項(xiàng)
上述試驗(yàn)的循環(huán)代碼如果在Release模式下會被C++編譯器優(yōu)化掉,因此需要在Debug模式下才能獲得預(yù)期效果,但在實(shí)際項(xiàng)目中大概率是不會被編譯器優(yōu)化的。
以上就是c++ 前自增/后自增操作符效率分析的詳細(xì)內(nèi)容,更多關(guān)于c++ 前自增/后自增操作符的資料請關(guān)注腳本之家其它相關(guān)文章!
- C++ 自增、自減運(yùn)算符的重載和性能分析小結(jié)
- 淺談C++類型轉(zhuǎn)化(運(yùn)算符重載函數(shù))和基本運(yùn)算符重載(自增自減)
- 基于C++輸出指針自增(++)運(yùn)算的示例分析
- 詳解C++賦值操作符重載
- C++-操作符重載、并實(shí)現(xiàn)復(fù)數(shù)類詳解
- 詳解C++-(=)賦值操作符、智能指針編寫
- C++ 開發(fā)之實(shí)現(xiàn)操作符重載的實(shí)例
- C++ 中placement new 操作符使用方法
- C++ operator關(guān)鍵字(重載操作符)的用法詳解
- C++ new、delete(new[]、delete[])操作符重載需要注意的問題
相關(guān)文章
C語言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn)
這篇文章主要介紹了C語言使用strcmp()函數(shù)比較兩個(gè)字符串的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01HDOJ 1443 約瑟夫環(huán)的最新應(yīng)用分析詳解
本篇文章是對HDOJ 1443 約瑟夫環(huán)的最新應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C語言中settimeofday函數(shù)和gettimeofday函數(shù)的使用
這篇文章主要介紹了C語言中的settimeofday函數(shù)和gettimeofday函數(shù)的使用,注意settimeofday()函數(shù)只返回0和-1,需要的朋友可以參考下2015-08-08C++?Cartographer源碼中關(guān)于MapBuilder的聲明與構(gòu)造
這篇文章主要介紹了C++?Cartographer源碼中關(guān)于MapBuilder的聲明與構(gòu)造,前面已經(jīng)談到了Cartographer中添加軌跡的方法和傳感器的數(shù)據(jù)流動(dòng)走向。在添加軌跡的時(shí)候,除了添加位姿估計(jì)器還有采樣器,訂閱回調(diào)函數(shù)之外,最重要的是通過map_builder_bridge添加了一條軌跡2023-03-03C++11的for循環(huán),以及范圍Range類的簡單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄狢++11的for循環(huán),以及范圍Range類的簡單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的, 現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06c++實(shí)現(xiàn)超簡單的貪吃蛇游戲?qū)嵗榻B
大家好,本篇文章主要講的是c++實(shí)現(xiàn)超簡單的貪吃蛇游戲?qū)嵗榻B,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12