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

前端 未结 5 1381
萌比男神i
萌比男神i 2021-01-02 18:14

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++)
          


        
5条回答
  •  梦毁少年i
    2021-01-02 18:34

    Considering that the array is huge, the amount of memory used, the indirections needed (a multidimensional array is a arrays of reference to arrays...), that does not seem at all slow to me. When you switch x and z you are probably trashing the cache.

    For comparison, you could store everything in a flat array.... This would improve the storing speed... but then the retrieval would be more complex and much more slow.

    int k = 0;
    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
                 arr[k++] = val;
            }
        }
    }
    

提交回复
热议问题