tags:

views:

39

answers:

1

We all know that jagged array performs better than multi-dimensional array, but what about jagged array and flatten array?

My intuition is that they should perform about the same. This is because both are direct access, unlike multi-dimensional array where you have to do some manipulation before you get to the element.

Am I right?

+2  A: 

The biggest difference is that a flattened array has the benefit that you only need to perform a single memory access to fetch or set a value. In a jagged array you need two memory accesses: one on the outer array, and one on the inner array.

A flattened array may also perform better than a jagged one since it will be allocated in a contiguous manner in memory. This means it's possible that locality of reference and CPU caching may help improve performance. In a jagged array, there's no guarantee that each sub-array is allocated nearby in memory, and would limit the benefits of cache locality.

In practice, the only way to answer performance questions is to try it both ways and measure the results.

LBushkin