简体   繁体   English

C++ 优化定义多维向量的性能

[英]C++ Optimize performance defining multidimensional vector

I'm having problem with time of elaboration with this code我对这段代码的详细阐述时间有疑问

vector <int>                        empty_a(120, 0);
vector <vector <int>>               empty_b(130, empty_a);
vector <vector <vector <int>>>      empty_c(220000, empty_b);
vector <vector <vector <vector <int>>>> res(3);

res[0]   =   empty_c;    
res[1]   =   empty_c;    
res[2]   =   empty_c;

This is the fastest way I know to define res equal to empty_c.这是我知道的将 res 定义为 empty_c 的最快方法。 It take too many time.这需要太多时间。 Also in:也在:

vector <vector <vector <int>>>      empty_c(220000, empty_b);

take a lot of time.花费很多时间。

Is there a fastest way?有没有最快的方法? I'm using also -O3 option.我也在使用 -O3 选项。

Thank's谢谢

Assuming your ints are 4 bytes, this vector represent at least 13 Gigabytes.假设你的整数是 4 个字节,这个向量至少代表 13 GB。

This is ridiculously large for most applications I can think of.对于我能想到的大多数应用程序来说,这都大得离谱。

Chances are you could get away with a sparse array.您有可能摆脱稀疏数组。 If not, once you get to such sizes, just allocate a single vector and use a mapping from 3D to 1D, like:如果没有,一旦达到这样的大小,只需分配一个向量并使用从 3D 到 1D 的映射,例如:

std::vector<int> flat(220000*120*130);
size_t At(size_t x, size_t y, size_t z) { return 120*130*z + 120*y+ x;}

flat[At(x,y,z)] = someValue;

Or use a specialized library that will handle all of this for you.或者使用一个专门的库来为你处理所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM