Quick access to the matrix

I need to access a two-dimensional matrix with C ++ code. If the matrix mat[n][m], I have to access (in the loop) these positions:

mat[x][y], mat[x-1][y-m-1], mat[x-1][y], mat[x][y-1]

In the next iteration, I have to do:

x=x+1

And again, again:

mat[x][y], mat[x-1][y-m-1], mat[x-1][y], mat[x][y-1]

What could be the best way for the closest memory positions to speed up my code?

+4
source share
3 answers

If you are iterating horizontally, position your matrix as mat [y] [x], especially if it is an array of arrays (the layout of the matrix is ​​unclear in your answer).

+1
source

Since you did not provide enough information, it is difficult to say which way is better.

You can try to expand the loop for continuous access to memory.

, mat[x][y] 4 , mat[x-1][y-m-1] 4 , mat[x-1][y] 4 , mat[x][y-1] 4 . 4 .

, . . SIMD-, 3/4 .

, . :

for( x=0; x<n; x++ )
    doSomething( mat[x][y] );

for( x=y; x<n*m; x+=m )
    doSomething( mat[0][x] );

lea.

0

, , x = x + 1 ( y). , i, 0 .

mat[i], mat[i-S-m-1], mat[i-S], mat[i-1]

S - ( ). , . / , i. , S+m+1 , .

0

Source: https://habr.com/ru/post/1533362/


All Articles