N
NigelW
This is really a question for the development team.
Are there plans to improve the optimization of C# to MSIL?
I ask this, as inspection with ILDASM of the MSIL code
shows that, even with the optimization switch on, there is
no inlining even of very short methods, common sub-
expressions are evaulated each time used and constants
used in loops are evaluated at each iteration. The
following code, where I hand optimized the source, runs
about twice as fast as the equivalent, original code shown
further down. I notice that the MSIL produced by the C++
compiler (with optimization on and the /clr flag set) does
have the optimizations. I'd appreciate your feedback as I
feel the relatively slow C# code is detering me moving
from C++ to C#.
double total = 0.0;
double recip1 = 1.0/999999.0;
for (int rep = 0; rep < 5; rep++)
{
total *= 0.001;
for (long i = 0; i <
100000000; i++)
{
double di = i;
total += di*recip1;
double disc =
total*total + di;
double root =
(total + disc)/(200000.0*(di + 1));
total -= root;
}
}
Unoptimized (original code):
double total = 0.0;
for (int rep = 0; rep < 5; rep++)
{
total /= 1000.0;
for (long i = 0; i <
100000000; i++)
{
total +=
i/999999.0;
double disc =
total*total + i;
double root =
(total + disc)/(200000.0*(i + 1));
total -= root;
}
}
Are there plans to improve the optimization of C# to MSIL?
I ask this, as inspection with ILDASM of the MSIL code
shows that, even with the optimization switch on, there is
no inlining even of very short methods, common sub-
expressions are evaulated each time used and constants
used in loops are evaluated at each iteration. The
following code, where I hand optimized the source, runs
about twice as fast as the equivalent, original code shown
further down. I notice that the MSIL produced by the C++
compiler (with optimization on and the /clr flag set) does
have the optimizations. I'd appreciate your feedback as I
feel the relatively slow C# code is detering me moving
from C++ to C#.
double total = 0.0;
double recip1 = 1.0/999999.0;
for (int rep = 0; rep < 5; rep++)
{
total *= 0.001;
for (long i = 0; i <
100000000; i++)
{
double di = i;
total += di*recip1;
double disc =
total*total + di;
double root =
(total + disc)/(200000.0*(di + 1));
total -= root;
}
}
Unoptimized (original code):
double total = 0.0;
for (int rep = 0; rep < 5; rep++)
{
total /= 1000.0;
for (long i = 0; i <
100000000; i++)
{
total +=
i/999999.0;
double disc =
total*total + i;
double root =
(total + disc)/(200000.0*(i + 1));
total -= root;
}
}