Performance comparison of array of arrays vs multidimensional arrays

后端 未结 4 1773
Happy的楠姐
Happy的楠姐 2020-12-18 03:18

When I was using C++ in college, I was told to use multidimensional arrays (hereby MDA) whenever possible, since it exhibits better memory locality since it\'s allocated in

4条回答
  •  盖世英雄少女心
    2020-12-18 03:47

    Java and C# allocate memory in much different fashion that C++ does. In fact, in .NET for sure all the arrays of AoA will be close together if they are allocated one after another because memory there is just one continuous chunk without any fragmentation whatsoever.

    But it is still true for C++ and still makes sense if you want maximum speed. Although you shouldn't follow that advise every time you want multidimensional array, you should write maintainable code first and then profile it if it is slow, premature optimization is root for all evil in this world.

提交回复
热议问题