环境:Window7 x64 ,Vs2015 Professional
硬件:内存 32.0GB 处理器 Inter(R) Core(TM) i7-4800MQ CPU @2.70GHz 2.70GHz
测试内容:数据类型 int ,遍历数量 count = 1e9 ,std::vector方法耗时:
序号 | 函数名 | Debug | Release |
1 | for(int i = 0;i<count;++i) | 1.7s | 0.0s |
2 | std::vector<int> data(count,1) | 173.6s | 1.1s |
3 | std::vector<int> data2 = data | 1.5s | 1.3s |
4 | for(int i = 0;i<count;++i) push_back(i) | 967.7s | 6.8s |
然后把 int 更改为 double ,Release 版本测试代码如下:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int count = 1e9;
int start = clock();
std::vector<double> values(count, 1.1);
int init = clock();
cout << "init time: "<<(init - start) << endl;
std::vector<double> values2 = values;
int copy = clock();
cout << "init time: "<<(copy - init) << endl;
std::vector<double> values3;
for (int i = 0; i < count; ++i)
{
values3.push_back(1.1);
}
int pushback = clock();
cout << "push_back time: " << (pushback - copy) << endl;
return 0;
}
当程序运行到push_back()时,停留在下图上:
360内存突然到达99%,电脑直接崩了......无奈重启电脑。
重启完进行测试,电脑没有崩掉,结果: