Fastest way to read/store lots of multidimensional data? (Java)

Posted by RemiX on Stack Overflow See other posts from Stack Overflow or by RemiX
Published on 2010-06-03T14:42:12Z Indexed on 2010/06/03 14:44 UTC
Read the original article Hit count: 131

I have three questions about three nested loops:

for (int x=0; x<400; x++)
{
    for (int y=0; y<300; y++)
    {
        for (int z=0; z<400; z++)
        {
             // compute and store value
        }
    }
}

And I need to store all computed values. My standard approach would be to use a 3D-array:

values[x][y][z] = 1; // test value

but this turns out to be slow: it takes 192 ms to complete this loop, where a single int-assignment

int value = 1; // test value

takes only 66 ms.

1) Why is an array so relatively slow?
2) And why does it get even slower when I put this in the inner loop:

values[z][y][x] = 1; // (notice x and z switched)

This takes more than 4 seconds!

3) Most importantly: Can I use a data structure that is as quick as the assignment of a single integer, but can store as much data as the 3D-array?

© Stack Overflow or respective owner

Related posts about java

Related posts about Performance