Jagged Array and Flatten Array which works better?

We all know that a jagged array works better than a multidimensional array, but what about a jagged and flatten array?

My intuition is that they should act approximately the same. This is due to the fact that both are direct access, unlike a multidimensional array, where you need to do some manipulations before moving on to the element.

I'm right?

+4
source share
1 answer

The biggest difference is that a flattened array has the advantage that you only need to do one memory access to retrieve or set a value. In a jagged array, you need two memory accesses: one to the external array and one to the internal array.

A solid array can also work better than a jagged array, since it will be allocated in a contiguous manner in memory. This means that referenced and CPU caching can help improve performance. A jagged array does not guarantee that each auxiliary array is located next to the memory and will limit the benefits of cache localization.

In practice, the only way to answer questions about performance is to try in both directions and measure the results.

+4
source

All Articles