(Richard Tobin) writes:
> In article <>,
> dimilar <> wrote:
>
>>for 2-dimension array, it is necessary to do much
> multiplication to obtain the
>>index of element.
>
> Certainly accessing a 2d array *conceptually* involves
> multiplication.
> But if you can straightforwardly use a 1d array instead,
> then quite
> likely so can the compiler. For example, in
>
> int a[n1][n2];
>
> for(i=0; i<n1; i++)
> for(j=0; j<n2; j++)
> ... do something with a[i][j] ...;
for the case you described here, multiplication is not a problem because you are
accessing elements in a certain order. but if we randomly access
many elements from a large 2d array. many multiplication is
inevitable. although I am also not sure it is a bottleneck.
however, I think it is really an algorithms dependent problem.
Thanks for you reply
> a reasonable compiler will not calculate i*n2+j from scratch
> each time
> round the inner loop. It can compute i*n2 once each time
> round the
> outer loop, and may not even do that.
>
> -- Richard