An efficient way to store a 2D matrix uses a C style array like this:
| a11 a12 a13 | | a21 a22 a23 | -> memory: [a11,a12,a13,a21,a22,a23,a31,a32,a33] | a31 a32 a33 | Element(i,j) = memory[N_COL*i+j]
where i is the index of the row number starting at 0 , and j index of the column number also starting at 0 and N_COL number of columns.
Hopefully the / jit compiler is about to put all the values โโsequentially into memory for quick access. Usually, the more you try to trick the compiler (for example, manually unrolling the loop), the more you feel in performance. Write clean code and let the compiler do its job.
source share