Faster access to matrix element (not really an opengl question)

Hi there,

I need to make thousands of access to matrix elements in an algorithm that usually spend hours running. So, I want to implement this as much optimized as I can.
Then, supose I implement the matrix M by N using a vector.

long double *Matrix;
// alloc memory as a vector.

In this case I would access an element (i,j) like this:

long double Element = *(Matrix + (i-1)*N + j - 1);

Now, supose I implement the same matrix M by N as:

long double **Matrix;
// alloc memory as a matrix.

In this case I would access an element (i,j) using:

long double Element = *( *(Matrix + (i-1)) + j - 1);

The “-1” is because my matrixs are not zero-based ( of course you know that )

So, which one you think it would be faster? (Remember it is done thousands of times)
If you know assembly and compilers or have a point about that, please let me know.

Thanks

1.Kill -1. It slows down greatly.
2.Make N a power of 2, so i*N will be done with shl(shift-left, << )
3.First way will be faster.
If you will not substitute multiplication with shift, maybe second will be faster.(i dont know what is worse - indirect adressing or multiplication)

Always try to access memory linearly, it helps your cache hit ratio. Secondly, instead of spending time figuring out how to speed up your matrix element access, spend time on figuring out how to avoid doing whatever you’re doing thousands of times for several hours. Optimising algorithms is usually much more effective than otimizing low level stuff.

I think I’ll use the first way (which is already working).
I’ll avoid the “-1”, it’s kind of easy to do that.

Any other ideas, please let me know.

Thank you!