- Home /
Jagged Arrays vs Multidimensional
I built my Minecraft-like block-world using 3d-Arrays of Objects (Block[x,y,z]).
Then i read everywhere that Jagged Arrays are much better performance wise. So I changed the whole code from 3d-Arrays zu Jagged Arrays with 3 Level depth (Block[x][y][z]).
It still works, but my benchmarking shows that EVERYTHING (from level build to block manipulation/Chunk rebuild) takes up to 2 times longer...
So now I'm reverting to the earlier version with 3D-Arrays.
Can somebody explain why Jagged Arrays actually seem to be slower than Multidimensional ones? Or is the problem that I changed all my foreach-loops to layered for-loops? That would be strange as well because for loops are supposed to be faster than foreach...
I'm confused...
Either way, doing a $$anonymous$$ecraft clone instantiating geometry will be very inefficient.
Why? I'm saving the vertice and uv positions in each block, then combine this information to the mesh for each chunk (normally 64x64x1 Blocks). And it Runs extremely fast, when I edit a block, it updates 3-6 Chunks and takes max. 0.004 seconds. What would you suggest to optimize (I know it sure isnt perfect but I dont see a better way than saving Block data in an Array of Objects, maybe just Arrays of Data)
Answer by Eric5h5 · Feb 24, 2012 at 12:40 AM
Accessing jagged arrays is over twice as fast as multi-dimensional arrays. When testing, you can only change one thing at a time, otherwise you don't know what changes caused the speed difference. However, the best performance is to use a 1D array rather than jagged or multi-d. So instead of
block[x, y, z]
or
block[x][y][z]
you would use
block[x + (width*length*y) + (width*z)]
It's not much faster than a jagged array, but when dealing with zillions of blocks, it will make at least a little difference.
Thank you. I think for the moment I'll just leave it as it is (with block[x,y,z]), since overall performance is quite good. Still, I find it very weird that everything ran much slower when I just changed the Arrays to Jagged...
Your answer
![](https://koobas.hobune.stream/wayback/20220613064418im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
pragma strict create List of Array of strings for class? 1 Answer
I don't know how to do multidimensional arrays 2 Answers
Can't access Array in a class instance multidimensional Array. 1 Answer
How do I save a three dimensional array? 1 Answer
print(var a = object) = null while print(oject) = what i want 2 Answers