optimization - Row / column vs linear indexing speed (spatial locality) -


related question: this one

i using spatial grid can potentially big (10^6 nodes) or bigger. regularly have perform displacement operations (like particle node another). i'm not crack in informatics begin understand concepts of cache lines , spatial locality, though not yet. so, wandering if preferible use 2d array (and if yes, one? i'd prefer avoid boost now, maybe link later) , indexing displacement example this:

array[i][j] -> array[i-1][j+2]

or, 1d array, if nx "equivalent" number of columns:

array[i*nx+j] -> array[(i-1)*nx+j+2]

knowing done 1 million times per iteration, 1 million iteration well.

with modern compilers , optimization enabled both of these generate exact same code

array[i-1][j+2]  // array 2-dimensional 

and

array[(i-1)*nx+j+2]  // array 1-dimensional 

assuming nx dimension of second subscript in 2-dimensional array (the number of columns).


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -