Search code examples
c++arraysperformancestdvectorboost-multi-array

Which is the fastest? A boost::multi_array or a std::vector?


Which is the fastest? A boost::multi_array or a std::vector? I will have (not constant) 17.179.869 elements stored in 3 dimensions which will need to be accessed inside a for loop very rapidly and very often. What would be the most performing? An std::vector or a boost::multi_array?

(I don't expect it to be done within a second, but I would like to have it as efficient as possible because a nanosecond difference can save a lot of time.)


Solution

  • Best advice is to benchmark it by yourself.

    In any case, since you seem to have constant size there are other solutions:

    • plain C arrays (eg. int data[X][Y][Z])
    • plain one dimensional C array in which you compute indices by yourself, eg X*W*H + Y*W + Z, can be handy in some situations
    • std::array, which is basically a C++ array with some synctactic sugar taken from STL collections
    • std::vector, which I guess is the first solution that can be tried
    • boost::multi_array, which is meant to support N dimensional arrays so it can be overkill for your purpose but probably has a better locality of data compared to a vector.