gpt4 book ai didi

c++ - std::vector 比普通数组慢很多吗?

转载 作者:IT老高 更新时间:2023-10-28 11:27:26 24 4
gpt4 key购买 nike

我一直认为 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

这大约慢了 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 " << 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;
}

是我做错了吗?还是我刚刚打破了这个性能神话?

我在 Visual Studio 2005 中使用 Release模式.


Visual C++ , #define _SECURE_SCL 0UseVector 减少一半(减少到 4 秒)。这真的很大,IMO。

最佳答案

使用以下内容:

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

所以数组的速度是 vector 的两倍。

但是在更详细地查看代码之后,这是意料之中的;当您两次运行 vector 并且仅运行一次数组时。注意:当你 resize() vector 时,你不仅在分配内存,而且还通过 vector 运行并调用每个成员的构造函数。

稍微重新排列代码,使 vector 只初始化每个对象一次:

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

现在再次做同样的时间:

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

vector 现在的性能只比数组差一点。 IMO 这种差异是微不足道的,可能是由一大堆与测试无关的事情引起的。

我还要考虑到您没有正确初始化/销毁 UseArrray() 方法中的 Pixel 对象,因为没有调用构造函数/析构函数(这可能不是问题简单的类但稍微复杂一点的类(即带有指针或带有指针的成员)都会导致问题。

关于c++ - std::vector 比普通数组慢很多吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3664272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com