Let's say I have class with field:
const double magicalConstant = 43;
This is somewhere in code:
double random = GetRandom();
double unicornAge = random * magicalConstant * 2.0;
Will compiler optimize my code so that it doesn't calculate magicalConstant * 2.0
every time it calcuates unicornAge
?
I know that I can define next const that takes this multiplication into account. But this looks much cleaner in my code. And it makes sense for compiler to optimize it.
(This question was the subject of my blog in October 2015; thanks for the interesting question!)
You have some good answers already that answer your factual question: No, the C# compiler does not generate the code to do a single multiplication by 86. It generates a multiplication by 43 and a multiplication by 2.
There are some subtleties here that no one has gone into though.
Multiplication is "left associative" in C#. That is,
must be computed as
And not
Now, is it the case that you ever get different answers for those two computations? If the answer is "no" then the operation is said to be an "associative operation" -- that is, it does not matter where we put the parentheses, and therefore can do optimizations to put the parentheses in the best place. (Note: I made an error in a previous edit of this answer where I said "commutative" when I meant "associative" -- a commutative operation is one where x * y is equal to y * x.)
In C#, string concatenation is an associative operation. If you say
then you get the same result for
And
and therefore the C# compiler can do an optimization here; it can do a computation at compile time and generate the code as though you had written
which is in fact what the C# compiler does. (Fun fact: implementing that optimization was one of the first things I did when I joined the compiler team.)
Is a similar optimization possible for multiplication? Only if multiplication is associative. But it is not! There are a few ways in which it is not.
Let's look at a slightly different case. Suppose we have
Can we just say that
is the same as
and generate a multiplication by 3.0? No. Suppose x is so small that x multiplied by 0.5 is rounded to zero. Then zero times 6.0 is still zero. So the first form can give zero, and the second form can give a non-zero value. Since the two operations give different results, the operation is not associative.
The C# compiler could have smarts added to it -- like I did for string concatenation -- to figure out in which cases multiplication is associative and do the optimization, but frankly it is simply not worth it. Saving on string concatenations is a huge win. String operations are expensive in time and memory. And it is very common for programs to contain very many string concatenations where constants and variables are mixed together. Floating point operations are very cheap in time and memory, it is hard to know which ones are associative, and it is rare to have long chains of multiplications in realistic programs. The time and energy it would take to design, implement and test that optimization would be better spent writing other features.