Easy to predict and hard to predict. Simple expressions, for example:
int a = b + (2 * c): int d = e + (2 * c);
optimized with the simplest optimizations (total subexpression (2 * c)
"will be calculated only once.
In C / C ++, methods declared in general will (though not always).
Trickier is loop optimization, etc. For instance,
for (int i = 1; i < n; i++) { a = i + (2 * c); }
expression (2 * c)
usually pulled out of a loop, in a compiler that performs "global optimization", but not in one that performs only "local optimization". And, of course, expressions can be much more complicated and confusing.
Change the body of the above loop to a = i * (2 * c);
and youβre moving to a slightly higher level of global optimization, known as loop induction. The smart compiler will only expect to add 2 * c
(as previously calculated) to a
for each iteration through the loop, instead of doing (more expensive) multiplication at each iteration.
And it just scratches the surface.
But I have no idea what Visual Studio compilers are capable of.
source share