简体   繁体   中英

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. 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.

Thank's

Assuming your ints are 4 bytes, this vector represent at least 13 Gigabytes.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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