I stumbled over an optimization (or lack of one, to be specific) when viewing
IL opcodes generated by the compiler using ms .net 2003. I was testing fast
pixel manipulation using Bitmap.LockBits and unsafe pointers to iterate over
an image's pixels. The inner-loop looked like this:
for (int x = 0; x < _buffer.Width*_ buffer.Height; x++)
{
// do manipulations here..
}
I noticed how the for-loop performed extremely slow, and ildasm revealed
that the "x < _buffer.Width*_ buffer.Height" was computed at each iteration,
resulting in two virtual calls and one mul. The use of /optimize did not
improve the matter, so the solution was to calculate
_buffer.Width*_ buffer.Height outside of the for-loop. I was under the
impression that the compiler performed hoisting automatically, am I missing
something here?
IL opcodes generated by the compiler using ms .net 2003. I was testing fast
pixel manipulation using Bitmap.LockBits and unsafe pointers to iterate over
an image's pixels. The inner-loop looked like this:
for (int x = 0; x < _buffer.Width*_ buffer.Height; x++)
{
// do manipulations here..
}
I noticed how the for-loop performed extremely slow, and ildasm revealed
that the "x < _buffer.Width*_ buffer.Height" was computed at each iteration,
resulting in two virtual calls and one mul. The use of /optimize did not
improve the matter, so the solution was to calculate
_buffer.Width*_ buffer.Height outside of the for-loop. I was under the
impression that the compiler performed hoisting automatically, am I missing
something here?
Comment