Sam Palmer
Sam Palmer

Reputation: 1725

Fastest way to access a table of data Java

Basically I am amidst a friendly code optimisation battle (to get the fastest program), I am trying to find a way that is faster to access a dictionary of hard coded data than a multidimensional array.

e.g to get the value for x:

int x = array[v1][v2][v3] ;

I have read that nested switch statements in a custom array may possibly be faster. Or is there a way I can possibly access memory more directly similar to pointers in C. Any ideas appreciated!

My 'competitor' is using a truth table and idea is to find something faster!

Many Thanks Sam

Upvotes: 2

Views: 155

Answers (1)

NPE
NPE

Reputation: 500367

If the array is regular in shape (i.e. MxNxK for some fixed M, N and K), you could try flattening it to achieve better locality of reference:

int array[] = new int[M*N*K];
...
int x = array[v1*N*K + v2*K + v3];

Also, if the entire array doesn't fit in the CPU cache, you might want to examine the patterns in which the array is accessed, to perhaps re-order the indices or change your code to make better use of the caches.

Upvotes: 4

Related Questions