- Home /
How significant is the performance difference between a jagged array and a multidimensional array?
I've heard many times that jagged arrays tend to have better access speeds and also that multidimensional arrays usually use less memory and can be allocated more quickly but exactly how big are these differences (in terms of actual numerical figures)?
Answer by MakeCodeNow · Jun 24, 2014 at 06:58 PM
I'm sure it depends on the language, runtime, platform, etc. In something like Mono/C# the differences are probably minuscule to non-existent. If need hard data, you should write a test yourself in your target environment, as other people's tests are not guaranteed to be identical. Ultimately, the choice in representation should be made based on which is a more natural expression of the functional logic. Don't worry about minuscule performance differences like this until they show up in a profiler (which they probably will not).
I did a few tests on my own and you seem to be right, there is usually very little difference between jagged and multidimensional arrays.
When it comes to access speed multidimensional arrays should be faster as the offset can be calculated directly. Jagged arrays you have to get the reference of the next inner array first before you can index the next dimension.
Jagged arrays have a slightly larger memory footprint in total but what most forget is that multidimensional arrays need 1 continuous block of memory while a jagged array is split up into smaller chunks of memory. If your memory is already heavily fragmented the creation of a multidimensional array might take a while or will completely fail.
It depends on the desired size and usage which one is better.
@$$anonymous$$akeCodeNow:
Since we talk about Unity the language is irrelevant since Unity only uses $$anonymous$$ono, on all platforms. On some platforms the implementation of $$anonymous$$ono might be different but that shouldn't make a big difference. It's more likely that the overall performance and memory size will make a difference.
Your answer
Follow this Question
Related Questions
RigidBodies vs CapsuleCasts 0 Answers
Scripting performance questions (JS) 0 Answers
Pros and Cons of Lightmapping 1 Answer
What's a good method for measuring shader performance? 0 Answers
Objects moving slowly in preview. 1 Answer