I am trying to compare the performance of boost::multi_array to native dynamically allocated arrays, with the following test program:
#include
Build in release mode, use objdump, and look at the assembly. They may be doing completely different things, and you'll be able to see which optimizations the compiler is using.
I would have expected multiarray to be just as efficient. But I'm getting similar results on a PPC Mac using gcc. I also tried multiarrayref, so that both versions were using the same storage with no difference. This is good to know, since I use multiarray in some of my code, and just assumed it was similar to hand-coding.
Another thing to try is to use iterators instead of a straight index for the boost array.
Looking at the assembly generated by g++ 4.8.2 with -O3 -DBOOST_DISABLE_ASSERTS
and using both the operator()
and the [][]
ways to access elements, it's evident that the only extra operation compared to native arrays and manual index calculation is the addition of the base. I didn't measure the cost of this though.