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[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…

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.