小编Bba*_*one的帖子

为什么这些结果表明不需要在向量中使用保留?

我编写了以下代码,以测量1000k不使用保留和保留的情况下推回整数时间所花费的时间。结果不是我想要的。

所有测试均在我的Samsung ativtap7上执行,该处理器具有在Windows 10下运行的核心i5 @ 1.8 Ghz处理器,4 GB RAM和VS2018 C ++编译器。

#include <iostream>
#include <vector>
#include "Stopwatch.h"
using namespace std;

int main()
{
    Stopwatch myWatch;
    //pushback 1000k times without reserve
    for (int i = 0; i < 10; i++)
    {
        cout << "try " << i + 1 << endl;
        myWatch.Start();
        vector<int> vec1;
        for (int i = 0; i < 1000000; i++)
        {
            vec1.push_back(i);
        }
        myWatch.End();
        myWatch.LookElapsedTime();

        //pushback 1000k times with reserve
        myWatch.Start();
        vector<int> vec2(1000000);
        for (int i …
Run Code Online (Sandbox Code Playgroud)

c++ performance stdvector c++11

-3
推荐指数
1
解决办法
145
查看次数

标签 统计

c++ ×1

c++11 ×1

performance ×1

stdvector ×1