Terr_•

the java compiler is smarter than you and can optimize your code better than you can

This is still the best default position to take, though. Sins of premature optimization, and all that. A bigger influence on how you order things should be the logical arrangement that makes sense depending on the data represented... and generally that coincides with the fast way. When it doesn't, that's a profiling issue.

7
n1tw1t•

Wow.. you could have won some money from me on this. Nice find.

3
kzn•

Hmm.. It's pretty obvious in fact, as T[][] is simply an array of arrays. Then it's trivial that the compiler can't optimize the layout. And they arrays may have different lengths.

1
tReP2pHu•

And this is the reason that most Java matrix libraries use 1D arrays for 2D matrices and access elements using T[row * size + column]

1
rtuck99•

The compiler may find it difficult to optimize that code because swapping the loop order will result in different values being stored in the array.

However, since the arrays are local and the output is never used, I suppose a really smart optimizer could actually optimize the loops completely away.

1