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ù)類型做測(cè)試
#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é)果可以看出來(lái),對(duì)int類型的測(cè)試中前自增和后自增耗費(fèi)時(shí)間基本不變;而對(duì)std::vector中iterator的測(cè)試顯示,前自增所耗費(fèi)的時(shí)間幾乎是后自增的一半。這是因?yàn)?,在后自增的操作中,?huì)首先生成原始對(duì)象的一個(gè)副本,然后將副本中的值加1后返回給調(diào)用者,這樣一來(lái)每執(zhí)行一次后自增操作,就會(huì)增加一個(gè)對(duì)象副本,效率自然降低了。
因此可以得出結(jié)論:對(duì)于C++內(nèi)置類型的自增而言,前自增、后自增的效率相差不大;對(duì)于自定義類型(類、結(jié)構(gòu)體)的自增操作而言,前自增的效率幾乎比后自增大一倍。
5、注意事項(xiàng)
上述試驗(yàn)的循環(huán)代碼如果在Release模式下會(huì)被C++編譯器優(yōu)化掉,因此需要在Debug模式下才能獲得預(yù)期效果,但在實(shí)際項(xiàng)目中大概率是不會(huì)被編譯器優(yōu)化的。
以上就是c++ 前自增/后自增操作符效率分析的詳細(xì)內(nèi)容,更多關(guān)于c++ 前自增/后自增操作符的資料請(qǐng)關(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++ 開(kāi)發(fā)之實(shí)現(xiàn)操作符重載的實(shí)例
- C++ 中placement new 操作符使用方法
- C++ operator關(guān)鍵字(重載操作符)的用法詳解
- C++ new、delete(new[]、delete[])操作符重載需要注意的問(wèn)題
相關(guān)文章
C++實(shí)現(xiàn)動(dòng)態(tài)數(shù)組功能
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)動(dòng)態(tài)數(shù)組功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
C++實(shí)現(xiàn)LeetCode(66.加一運(yùn)算)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(66.加一運(yùn)算),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
c語(yǔ)言實(shí)現(xiàn)整蠱朋友小程序(附源碼)
這篇文章主要給大家介紹了關(guān)于c語(yǔ)言實(shí)現(xiàn)整蠱朋友小程序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
C 與 C++ 接口函數(shù)相互調(diào)用的實(shí)現(xiàn)
這篇文章主要介紹了C 與 C++ 接口函數(shù)相互調(diào)用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
C++實(shí)現(xiàn)簡(jiǎn)單推箱子小游戲
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單推箱子小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
c++中explicit與mutable關(guān)鍵字的深入探究
這篇文章主要給大家介紹了關(guān)于c++中explicit與mutable關(guān)鍵字的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05

