为std :: vector的比普通阵列这么多慢? [英] Is std::vector so much slower than plain arrays?

查看:101
本文介绍了为std :: vector的比普通阵列这么多慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为这是一般的智慧的std ::矢量是实现为一个数组,等等等等。今天我去了和测试它,它似乎不是这样:

I've always thought it's the general wisdom that std::vector is "implemented as an array," blah blah blah. Today I went down and tested it, and it seems to be not so:

下面是一些测试结果:

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

这是约3 - 4倍慢!没有真正的理由为矢量可几nanosecs要慢的意见。

That's about 3 - 4 times slower! Doesn't really justify for the "vector may be slower for a few nanosecs" comments.

而code我用:

#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 " << d.total_milliseconds() / 1000.0 <<
                " seconds" << endl;
        }

    private:
        std::string name;
        boost::posix_time::ptime start;
};

struct Pixel
{
    Pixel()
    {
    }

    Pixel(unsigned char r, unsigned char g, unsigned char b) : r(r), g(g), b(b)
    {
    }

    unsigned char r, g, b;
};

void UseVector()
{
    TestTimer t("UseVector");

    for(int i = 0; i < 1000; ++i)
    {
        int dimension = 999;

        std::vector<Pixel> pixels;
        pixels.resize(dimension * dimension);

        for(int i = 0; i < dimension * dimension; ++i)
        {
            pixels[i].r = 255;
            pixels[i].g = 0;
            pixels[i].b = 0;
        }
    }
}

void UseVectorPushBack()
{
    TestTimer t("UseVectorPushBack");

    for(int i = 0; i < 1000; ++i)
    {
        int dimension = 999;

        std::vector<Pixel> pixels;
            pixels.reserve(dimension * dimension);

        for(int i = 0; i < dimension * dimension; ++i)
            pixels.push_back(Pixel(255, 0, 0));
    }
}

void UseArray()
{
    TestTimer t("UseArray");

    for(int i = 0; i < 1000; ++i)
    {
        int dimension = 999;

        Pixel * pixels = (Pixel *)malloc(sizeof(Pixel) * dimension * dimension);

        for(int i = 0 ; i < dimension * dimension; ++i)
        {
            pixels[i].r = 255;
            pixels[i].g = 0;
            pixels[i].b = 0;
        }

        free(pixels);
    }
}

int main()
{
    TestTimer t1("The whole thing");

    UseArray();
    UseVector();
    UseVectorPushBack();

    return 0;
}

我是不是做错了什么?或者我刚刚破获这一业绩神话?

Am I doing it wrong or something? Or have I just busted this performance myth?

我在<一个使用拍摄模式href=\"http://en.wikipedia.org/wiki/Microsoft_Visual_Studio#Visual_Studio_2005\">Visual Studio 2005.

的Visual C ++ 的#define _SECURE_SCL 0 减少 UseVector (把它降低到4秒)。这的确是巨大的,海事组织。

In Visual C++, #define _SECURE_SCL 0 reduces UseVector by half (bringing it down to 4 seconds). This is really huge, IMO.

推荐答案

使用以下内容:

G ++ -O3 Time.cpp -I&LT; MyBoost&GT;结果
  ./a.out结果
  UseArray在2.196秒完成结果
  UseVector在4.412秒完成结果
  在8.017秒UseVectorPushBack完成结果
  在整个事件中14.626秒完成

g++ -O3 Time.cpp -I <MyBoost>
./a.out
UseArray completed in 2.196 seconds
UseVector completed in 4.412 seconds
UseVectorPushBack completed in 8.017 seconds
The whole thing completed in 14.626 seconds

所以阵列的两倍,矢量一样快。

So array is twice as quick as vector.

但是<强/>详细看code后,此预期;如你横跨矢量两次和阵列仅运行一次。注意:当调整()的载体,你不仅分配内存还可以通过矢量运行,并呼吁各成员在构造

But after looking at the code in more detail this is expected; as you run across the vector twice and the array only once. Note: when you resize() the vector you are not only allocating the memory but also running through the vector and calling the constructor on each member.

重新排列code稍微使得矢量只初始化每个对象一次:

Re-Arranging the code slightly so that the vector only initializes each object once:

 std::vector<Pixel>  pixels(dimensions * dimensions, Pixel(255,0,0));

现在又在做同样的时机:

Now doing the same timing again:

G ++ -O3 Time.cpp -I&LT; MyBoost&GT;结果
  ./a.out结果
  UseVector在2.216秒完成

g++ -O3 Time.cpp -I <MyBoost>
./a.out
UseVector completed in 2.216 seconds

矢量现在只有性能比阵列略差。 IMO这种差异是微不足道的,可以由一大堆不与测试相关的事情所引起的。

The vector now performance only slightly worse than the array. IMO this difference is insignificant and could be caused by a whole bunch of things not associated with the test.

我还要考虑到你是不是正确初始化/在UseArrray()方法既不构造/析构不叫销毁像素对象(这可能不是这个简单的类,但任何稍微复杂的问题(即指针或成员指针)会引起问题。

I would also take into account that you are not correctly initializing/Destroying the Pixel object in the UseArrray() method as neither constructor/destructor is not called (this may not be an issue for this simple class but anything slightly more complex (ie with pointers or members with pointers) will cause problems.

这篇关于为std :: vector的比普通阵列这么多慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆