Robert Jacques wrote:
I haven't seen I compiler yet to do that optimization and DMD 1.038
doesn't:
foreach(ref val; array) val /= x;
is ten times slower than
foreach(ref val; array) val *= invx;
on a 100,000 element array.
The reason the optimizer doesn't replace floating divides with floating
multiplies of the reciprocal is that one gets slightly different answers
(due to roundoff).
My general rule of thumb is than the optimizer is pretty good within a
single code block and less so across blocks. And there's really no
substitute for profiling and testing the user-optimization to see if
it's actually worthwhile.
It's also straightforward to run obj2asm on the output and see what it's
doing.