我是靠谱客的博主 纯情悟空,这篇文章主要介绍c++ 前自增/后自增操作符效率分析,现在分享给大家,希望可以做个参考。

1、前自增/后自增操作符示例

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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、分别基于内置数据类型和自定义数据类型做测试

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#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、五次执行结果

4、结果分析及结论

从上面的执行结果可以看出来,对int类型的测试中前自增和后自增耗费时间基本不变;而对std::vector中iterator的测试显示,前自增所耗费的时间几乎是后自增的一半。这是因为,在后自增的操作中,会首先生成原始对象的一个副本,然后将副本中的值加1后返回给调用者,这样一来每执行一次后自增操作,就会增加一个对象副本,效率自然降低了。

因此可以得出结论:对于C++内置类型的自增而言,前自增、后自增的效率相差不大;对于自定义类型(类、结构体)的自增操作而言,前自增的效率几乎比后自增大一倍。

5、注意事项

上述试验的循环代码如果在Release模式下会被C++编译器优化掉,因此需要在Debug模式下才能获得预期效果,但在实际项目中大概率是不会被编译器优化的。

以上就是c++ 前自增/后自增操作符效率分析的详细内容,更多关于c++ 前自增/后自增操作符的资料请关注靠谱客其它相关文章!

最后

以上就是纯情悟空最近收集整理的关于c++ 前自增/后自增操作符效率分析的全部内容,更多相关c++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(114)

评论列表共有 0 条评论

立即
投稿
返回
顶部