Re: JIT Optimizations Lacking!
- From: Barry Kelly <barry.j.kelly@xxxxxxxxx>
- Date: Mon, 18 Aug 2008 21:37:44 +0100
Nathan Zaugg <Nathan Zaugg@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I have been doing some research on Managed code v.s. Native code. In my
first battery of tests I have realized that the optimization
performed during the JIT compilation is not nearly as good as the
optimizations done by C++ at compile time.
Here are a couple of examples I have found so far:
C# Code:
Your benchmark does no meaningful work (i.e. it is unrepresentative of
the vast majority of programs). You can't conclude much of consequence
from it.
static void Main(string[] args) {
Stopwatch sw = Stopwatch.StartNew();
int i = 123456789;
// Do Work!
for (int j = 0; j < 1000000000; j++) {
i = (i << 2) + 1525885;
}
sw.Stop();
// Must use the result to prevent it from being optimized out
Console.WriteLine("Result: " + i);
Console.WriteLine("Elapsed: " + sw.Elapsed.TotalMilliseconds);
There is also a bug in the optimization engine. For example if we remove
the statements that output the result (i)
the C++ code completely removes all of the for loop and returns in 0ms. The
C# version removes the "work" aspect
C++ has a preprocessor, and some of the idioms of preprocessor usage
*intentionally* generate code which does no work - i.e. an if-statement
with a condition that is statically false, or a while-loop that is never
entered, etc. These idioms are frequently used for selecting between
debugging builds and optimized release builds, or light logging versus
verbose logging, for example.
On the other hand, C# does not have such a strong tradition of
generating pointless code, as it has other tools at its disposal (JIT in
debug mode versus normal; runtime loading of classes & configuration
rather than compile-time configuration).
So, what you are observing is the result of the developers behind the
CLR JIT and MSVC putting work in where it is likely to have most effect,
rather than trying to optimize useless code.
Are there any plans in the works do improve the JIT compilation?
Every software company interested in selling the next iteration of its
technology is interested in improving performance; I'm sure the CLR team
is no different in this respect.
Also, bear in mind that the MSVC team have had at least a decade longer
to work on optimizations, and haven't had to live under the constraints
of the JIT.
-- Barry
--
http://barrkel.blogspot.com/
.
- Follow-Ups:
- Re: JIT Optimizations Lacking!
- From: Nathan Zaugg
- Re: JIT Optimizations Lacking!
- References:
- JIT Optimizations Lacking!
- From: Nathan Zaugg
- JIT Optimizations Lacking!
- Prev by Date: JIT Optimizations Lacking!
- Next by Date: Re: JIT Optimizations Lacking!
- Previous by thread: JIT Optimizations Lacking!
- Next by thread: Re: JIT Optimizations Lacking!
- Index(es):
Relevant Pages
|
Loading