Given the following expression in C#, and that chunkWidth and chunkHeight are fixed pre-calculated numbers, is it possible to optimise the expression by perhaps pre-computing part of the modulo division beforehand?
// Once assigned these guys never change
private int _chunkWidth;
private int _chunkHeight;
// This function needs to be super optimal!
SomeObject LookupObject(int row, int column) {
int index = (row % _chunkHeight) * _chunkWidth + (column % _chunkWidth);
return _objects[ index ];
}
Even if
chunkWidthandchunkHeightare fixed and pre-calculated fields, but still you cannot further optimize the modulo operation sincerowandcolumnare variables which requires modulo operation each time the statement is executed.