What are the differences between multidimensional arrays double[,]
and array-of-arrays double[][]
in C#?
If there is a difference, what is the best use for each one?
What are the differences between multidimensional arrays double[,]
and array-of-arrays double[][]
in C#?
If there is a difference, what is the best use for each one?
Array of arrays (jagged arrays) are faster than multi-dimensional arrays and can be used more effectively. Multidimensional arrays have nicer syntax.
If you write some simple code using jagged and multidimensional arrays and then inspect the compiled assembly with an IL disassembler you will see that the storage and retrieval from jagged (or single dimensional) arrays are simple IL instructions while the same operations for multidimensional arrays are method invocations which are always slower.
Consider the following methods:
Their IL will be the following:
When using jagged arrays you can easily perform such operations as row swap and row resize. Maybe in some cases usage of multidimensional arrays will be more safe, but even Microsoft FxCop tells that jagged arrays should be used instead of multidimensional when you use it to analyse your projects.
In addition to the other answers, note that a multidimensional array is allocated as one big chunky object on the heap. This has some implications:
<gcAllowVeryLargeObjects>
for multidimensional arrays way before the issue will ever come up if you only ever use jagged arrays.A multidimensional array creates a nice linear memory layout while a jagged array implies several extra levels of indirection.
Looking up the value
jagged[3][6]
in a jagged arrayvar jagged = new int[10][5]
works like this: Look up the element at index 3 (which is an array) and look up the element at index 6 in that array (which is a value). For each dimension in this case, there's an additional look up (this is an expensive memory access pattern).A multidimensional array is laid out linearly in memory, the actual value is found by multiplying together the indexes. However, given the array
var mult = new int[10,30]
, theLength
property of that multidimensional array returns the total number of elements i.e. 10 * 30 = 300.The
Rank
property of a jagged array is always 1, but a multidimensional array can have any rank. TheGetLength
method of any array can be used to get the length of each dimension. For the multidimensional array in this examplemult.GetLength(1)
returns 30.Indexing the multidimensional array is faster. e.g. given the multidimensional array in this example
mult[1,7]
= 30 * 1 + 7 = 37, get the element at that index 37. This is a better memory access pattern because only one memory location is involved, which is the base address of the array.A multidimensional array therefore allocates a continuous memory block, while a jagged array does not have to be square, e.g.
jagged[1].Length
does not have to equaljagged[2].Length
, which would be true for any multidimensional array.Performance
Performance wise, multidimensional arrays should be faster. A lot faster, but due to a really bad CLR implementation they are not.
The first row are timings of jagged arrays, the second shows multidimensional arrays and the third, well that's how it should be. The program is shown below, FYI this was tested running mono. (The windows timings are vastly different, mostly due to the CLR implementation variations).
On windows, the timings of the jagged arrays are greatly superior, about the same as my own interpretation of what multidimensional array look up should be like, see 'Single()'. Sadly the windows JIT-compiler is really stupid, and this unfortunately makes these performance discussions difficult, there are too many inconsistencies.
These are the timings I got on windows, same deal here, the first row are jagged arrays, second multidimensional and third my own implementation of multidimensional, note how much slower this is on windows compared to mono.
Source code:
Simply put multidimensional arrays are similar to a table in DBMS.
Array of Array (jagged array) lets you have each element hold another array of the same type of variable length.
So, if you are sure that the structure of data looks like a table (fixed rows/columns), you can use a multi-dimensional array. Jagged array are fixed elements & each element can hold an array of variable length
E.g. Psuedocode:
Think of the above as a 2x2 table:
Think of the above as each row having variable number of columns:
I would like to update on this, because in .NET Core multi-dimensional arrays are faster than jagged arrays. I ran the tests from John Leidegren and these are the results on .NET Core 2.0 preview 2. I increased the dimension value to make any possible influences from background apps less visible.
I looked into disassemblies and this is what I found
jagged[i][j][k] = i * j * k;
needed 34 instructions to executemulti[i, j, k] = i * j * k;
needed 11 instructions to executesingle[i * dim * dim + j * dim + k] = i * j * k;
needed 23 instructions to executeI wasn't able to identify why single-dimensional arrays were still faster than multi-dimensional but my guess is that it has to do with some optimalization made on the CPU
This might have been mentioned in the above answers but not explicitly: with jagged array you can use
array[row]
to refer a whole row of data, but this is not allowed for multi-d arrays.