初始化3D向量的最有效方法是什么? [英] What is the most efficient way to initialize a 3D vector?

查看:56
本文介绍了初始化3D向量的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中有一个3D字符串向量:

I have a 3D string vector in C++:

vector<vector<vector<string>>> some_vector

我正在尝试找到一种快速的方法来为其分配内存.

That I am trying is to find a fast method to allocate memory for it.

我尝试使用以下两种不同的方法来定义它:

I tried to define it with two different methods as follow:

#include<vector>
#include<iostream>
#include<ctime>
using namespace std;

#define DIM1 100
#define DIM2 9
#define DIM3 120

int main()
{
    clock_t t1_start = clock();
    vector<vector<vector<string>>> vec1(DIM1, vector<vector<string>>(DIM2, vector<string>(DIM3)));
    clock_t t1_end = clock();
    double diff1 = (t1_end - t1_start) / double(CLOCKS_PER_SEC);

    clock_t t2_start = clock();
    vector<vector<vector<string>>> vec2;
    vec2.resize(DIM1);
    for(int i = 0; i < DIM1; i++)
    {
        vec2[i].resize(DIM2);
        for(int j = 0; j < DIM2; j++)
            vec2[i][j].resize(DIM3);
    }
    clock_t t2_end = clock();

    double diff2 = (t2_end - t2_start) / double(CLOCKS_PER_SEC);

    cout<<"1st definition used time: "<<diff1<<"s"<<endl;
    cout<<"2nd definition used time: "<<diff2<<"s"<<endl;
}

我希望第一种方法(vec1)可能比第二种方法(vec2)快.

I expect that the first method (vec1) could be faster than the 2nd one (vec2).

但是事实证明,第一种方法比第二种方法要慢得多.在我的机器上,第一种方法使用0.245秒,而第二种方法使用0.152秒.

But it turned out that the 1st method is much slower than the 2nd. On my machine, the 1st method used 0.245 seconds, while the 2nd method used 0.152 seconds.

此外,当我将数据类型切换为int时,第一个花费0.058秒,第二个花费0.004.

Moreover, when I switch the data type to int, the 1st one took 0.058 second, and the 2nd took 0.004.

我可以知道是什么原因造成这种差异吗?还有更好的方法为3D向量分配内存吗?

May I know what cause such difference? And is there better way to allocate memory for a 3D vector?

非常感谢.

推荐答案

我可以知道是什么原因造成这种差异吗?

May I know what cause such difference?

第一个版本通过复制1-d向量来构建2-d向量,然后通过复制来构建3-d向量.这可能比不复制而调整向量的大小要慢.但是,我希望如果您进行优化时,差异可以忽略不计.

The first version constructs a 2-d vector by copying a 1-d vector, and then constructs the 3-d vector by copying that. This might be slower than resizing the vectors without copying. However, I'd hope that the difference would be negligible if you're building with optimisation.

还有更好的方法为3D向量分配内存吗?

And is there better way to allocate memory for a 3D vector?

最好使用单个连续数组,将其包装在提供多维访问器的类中.这将使分配更加简单,并且在访问元素时也将避免某些指针取消引用(以一些算术为代价).像这样:

It might be better to use a single contiguous array, wrapped in a class that provides multi-dimensional accessors. This would make allocation much simpler, and would also avoid some pointer dereferencing when accessing elements (at the cost of a bit of arithmetic). Something like this:

template <typename T>
class vector3d {
public:
    vector3d(size_t d1=0, size_t d2=0, size_t d3=0, T const & t=T()) :
        d1(d1), d2(d2), d3(d3), data(d1*d2*d3, t)
    {}

    T & operator()(size_t i, size_t j, size_t k) {
        return data[i*d2*d3 + j*d3 + k];
    }

    T const & operator()(size_t i, size_t j, size_t k) const {
        return data[i*d2*d3 + j*d3 + k];
    }

private:
    size_t d1,d2,d3;
    std::vector<T> data;
};

这篇关于初始化3D向量的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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