8 ms·
To be fair, there may be very good data locality reasons to prefer myArray[w*y+x] over myArray[x][y] if you're trying to construct a rectangular 2d array. The
by marginalia_nu 3y ago
To be fair, there may be very good data locality reasons to prefer myArray[w*y+x] over myArray[x][y] if you're trying to construct a rectangular 2d array. The two forms are typically not equivalent. One is an array of arrays (i.e. double indirection), and the other is not.
The loops I don't have an explanation for though.
- jszymborski 3y agoGood point! The loops, however, don't inspire the sort of confidence that would lead me to personally believe this person was concerned about data locality. I might be wrong though hehe I think that this is a good reminder, however, that there are few "absolutes" when it comes to programming and that there are always edge cases where some things that look like "code smells" or even look plain wrong have their place.
- amomchilov 3y agoIf you design your abstractions correctly, you can get the best of both worlds. You can create a simple Matrix type which stores single array, with a subscript operator that gives a 2D facade. It can let you call it with x/y pairs, but just lower that to an index into your single contiguous buffer.
- marginalia_nu 3y agoYes, depending on the language.