简体   繁体   中英

Performance of std::vector with 4 dimensions

I am looking for the fastest way to save double values in a data structure which runs over 4 indexes.

I tried it with std::vector in the following way:

vector<vector<vector<vector<double>>>> myValues

My program has a lot of operations in which the values of mValues are read and changed and I would like to improve the performance.

How fast is this 4-dimensional vector structure? Are there better options which would be more appropriate?

Thank you very much.

Internally, a vector stores its data in a dynamically allocated array, so a 4 level vector will have same performances an array of pointers to arrays of pointers to arrays of pointers to arrays of double values.

The alternative would be to allocate a 4D linear array (possibly through a vector) and manually do the index computations (index of arr[i][j][k][l] is l + level4_size * (k + level3_size * (j + level2_size * i)) , if will be certainly more efficient in memory usage, unsure for speed because it could depends of the actual accesses. If you really care for speed, you should benchmark both implementations in your actual environment.

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