Giuseppe Dini
Giuseppe Dini

Reputation: 762

Accessing data from arrays very quickly

My problem is here.
I have four 3-dimensional tables which are a sort of look up tables ( 32x32x32 ) where I have to retrieve values in real time and very quickly in c++.
My main goal is computational time as a very large amount of operations have to be made, and good programming practice is secondary for this, as I’m less worried about code maintaining. What is the best solution on the majority of personal computer to achieve this?
I’ve thought of storing the data on global variables and put them in a separate file and using them through declarations like “extern const float first[32][32][32];” or loading a vector from a file.
But the latter, I suppose, would be slower because data have to accessed through one more pointer.
Any other solution?

I made a search on the web, but this confused me more as I read different truths.
For example this two websites don’t seem to suggest the same thing (if I understand correctly): site1 , site2

Upvotes: 2

Views: 440

Answers (2)

ecatmur
ecatmur

Reputation: 157424

Accessing data in a multidimensional array

float data[32][32][32];
data[i][j][k];

is exactly equivalent to accessing a flattened array

float data[32768];
data[i * 1024 + j * 32 + k];

There is no extra pointer involved, just arithmetic on the array indexes.

In terms of performance, an extern const float [32][32][32] will offer the compiler opportunities for whole program optimisation as it will have full access to the data and how you are using it.

Loading data from a file into a dynamic-sized vector<vector<vector<float>>> would result in multiple pointer indirections to access data elements; given that you know the size of the data, loading it into a static float[32][32][32] would be more efficient.

For data where the extents are unknown at compile time, a good option would be to use Boost.MultiArray:

typedef boost::multi_array<float, 3> array_type;
array_type A(boost::extents[32][32][32]);

A[i][j][k] = 5.0f;

Upvotes: 2

Ram
Ram

Reputation: 3143

If you are performing an operation on each element, there is a fair chance that this can be performed in parallel. Since you are interested specifically in optimizing access time, you can consider treating the array as a 1D array for faster access.

   struct Object32P3 {
     union { 
    struct {float arr3[32][32][32]}; 
    struct {float arr1[32768]}; 
    }; 
    }; 

Accessing 1D array by offset is faster than a 3d array in tight loops.

the reason I say that accessing should not be the first place you have to try and optimize is because, you are performing some operation after accessing the elements and that is the best place to look and optimize.

Upvotes: 2

Related Questions