Compiler optimization of repeated accessor calls
Posted
by apocalypse9
on Stack Overflow
See other posts from Stack Overflow
or by apocalypse9
Published on 2010-03-23T02:09:57Z
Indexed on
2010/03/23
5:01 UTC
Read the original article
Hit count: 457
I've found recently that for some types of financial calculations that the following pattern is much easier to follow and test especially in situations where we may need to get numbers from various stages of the computation.
public class nonsensical_calculator
{
...
double _rate;
int _term;
int _days;
double monthlyRate { get { return _rate / 12; }}
public double days { get { return (1 - i); }}
double ar { get { return (1+ days) /(monthlyRate * days)
double bleh { get { return Math.Pow(ar - days, _term)
public double raar { get { return bleh * ar/2 * ar / days; }}
....
}
Obviously this often results in multiple calls to the same accessor within a given formula. I was curious as to whether or not the compiler is smart enough to optimize away these repeated calls with no intervening change in state, or whether this style is causing a decent performance hit.
Further reading suggestions are always appreciated
© Stack Overflow or respective owner