相关疑难解决方法(0)

std :: vector比普通数组慢得多吗?

我一直认为这std::vector是"作为阵列实施的一般智慧",等等等等等等.今天我去了测试它,似乎不是这样:

这是一些测试结果:

UseArray completed in 2.619 seconds
UseVector completed in 9.284 seconds
UseVectorPushBack completed in 14.669 seconds
The whole thing completed in 26.591 seconds
Run Code Online (Sandbox Code Playgroud)

这大约慢了3-4倍!没有真正证明" vector可能会慢几纳米"的评论.

我使用的代码:

#include <cstdlib>
#include <vector>

#include <iostream>
#include <string>

#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/microsec_time_clock.hpp>

class TestTimer
{
    public:
        TestTimer(const std::string & name) : name(name),
            start(boost::date_time::microsec_clock<boost::posix_time::ptime>::local_time())
        {
        }

        ~TestTimer()
        {
            using namespace std;
            using namespace boost;

            posix_time::ptime now(date_time::microsec_clock<posix_time::ptime>::local_time());
            posix_time::time_duration d = now - start;

            cout << name << " completed in " << …
Run Code Online (Sandbox Code Playgroud)

c++ arrays performance stl vector

203
推荐指数
7
解决办法
8万
查看次数

标签 统计

arrays ×1

c++ ×1

performance ×1

stl ×1

vector ×1