Re: Can C# be as fast as C++?
- From: "Shawn B." <leabre@xxxxxxxx>
- Date: Tue, 5 Feb 2008 13:46:48 -0800
There's a significant possibility that the machine specific optimizations
are only done during JIT, as NGEN places the result in a file which can
be
moved around.
I believe the ngen'd file has a record of various machine-specific
attributes (such as processor type) and will be ignored if they're
wrong. However, I've *also* heard that ngen doesn't have all of the
optimizations of the normal JIT - I'm not sure how to square these
two. I suppose the normal JIT is able to do cross-assembly inlining
which could be tricky in ngen, etc.
This also depends. NGEN is different for both 64-bit and 32-bit Intel/AMD
CPU's. The JIT for 32-bit was written by the C# team, IIRC, and the C++
team wrote the JIT/NGEN for 64-bit so its much more in-tune with the C++
optimimizer <grin>. IIRC, the 64-bit JIT/NGEN was written from scratch. At
least this is what I read and I read it a while ago just before Whidbey was
released.
However, when I look at the code generation for C# on both 32/64-bit
versions, the C# compiler doesn't seem to produce as much optimal IL
instructions as the C++/CLI compiler (for obvious reasons). What is
interesting that if I hand-tuned some IL instructions from C# programs (for
example, changing some things in ways that would remove, say, 8 out of 49 IL
instructions, or simply rearrage the sequence of instructions, on 32-bit
JIT, during profiling, would produce worse for some reason but on 64-bit
performs better. Hard to know exactly why. I figured that the 32-bit JIT
looked for patterns that C#/VB.NET (nearly identicle output) produce and
optimizes that and by hand-tuning some IL it broke the pattern recognition
but, in reality, I have no idea whether that is the truth.
Having written my own C# compiler that allows me to dynamically add new
syntax and language features on a per-project or per-assembly
(referenceable) basis, I've sound ways to write IL that produce good
results. I had to spend insane amounts of time looking at what C#/VBn/C++
CLI produced and figured out what works best. In the end, I produce my own
opcode sequences in some areas where I found advantages in doing so.
Now all we need is a way for IL to express something that very easily maps
to SSE2/3/4//altivec/etc.. so I can optimize some of my numericals.
Thanks,
Shawn
.
- Follow-Ups:
- fao Shawn B : Re: Can C# be as fast as C++?
- From: Bill Woodruff
- fao Shawn B : Re: Can C# be as fast as C++?
- References:
- Re: Can C# be as fast as C++?
- From: Ben Voigt [C++ MVP]
- Re: Can C# be as fast as C++?
- From: Jon Skeet [C# MVP]
- Re: Can C# be as fast as C++?
- Prev by Date: Somewhat of an academic challange (but usefull if solved)
- Next by Date: Re: 100K item data binding: Is asynchronous data binding possible?
- Previous by thread: Re: Can C# be as fast as C++?
- Next by thread: fao Shawn B : Re: Can C# be as fast as C++?
- Index(es):
Relevant Pages
|