简体   繁体   中英

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.

eg 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

If the array is regular in shape (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM