bearophile wrote:
While writing code that works on matrices I have found something
curious, so I have written the following little benchmark. As usual
keep eyes open for possible bugs and mistakes of mine:
[snip]

This is yet another proof that bounds checking can cost a lot. Although the looping code looks the same, in the case of using arrays there are plenty of bounds checks going on. My guess is that if you turn that off, the differences won't be as large (or even detectable for certain ranges of N).

--------------------------
> Note there are much faster ways to perform A * B:
>
> // Alternative mul code
> double[] bj = new double[N];
> for (int j = 0; j < N; j++) {
>     for (int k = 0; k < N; k++)
>         bj[k] = B[k][j];
>     for (int i = 0; i < N; i++) {
>         double[] ai = A[i];
>         double s = 0;
>         for (int k = 0; k < N; k++)
>             s += ai[k] * bj[k];
>         C[i][j] = s;
>     }
> }

Probably blocking will bring even more mileage (but again that depends on N).


Andrei

Reply via email to