Will an optimizing compiler remove calls to a method whose result will be multiplied by zero?

Posted by Tim R. on Stack Overflow See other posts from Stack Overflow or by Tim R.
Published on 2010-12-22T07:01:44Z Indexed on 2010/12/31 21:54 UTC
Read the original article Hit count: 190

Filed under:

Suppose you have a computationally expensive method, Compute(p), which returns some float, and another method, Falloff(p), which returns another float from zero to one.

If you compute Falloff(p) * Compute(p), will Compute(p) still run when Falloff(p) returns zero? Or would you need to write a special case to prevent Compute(p) from running unnecessarily?

Theoretically, an optimizing compiler could determine that omitting Compute when Falloff returns zero would have no effect on the program. However, this is kind of hard to test, since if you have Compute output some debug data to determine whether it is running, the compiler would know not to omit it because of that debug info, resulting in sort of a Schrodinger's cat situation.

I know the safe solution to this problem is just to add the special case, but I'm just curious.

© Stack Overflow or respective owner

Related posts about optimization