   Why is .NET slower than normally compiled code(C++)?
  I have been wondering for some time why .NET languages can’t beat or compete with C++ in terms of performance. I think it might be because .NET languages have stuff like garbage collection, and that slows them down (you got to admit that it is slower in most cases), also that you in most .NET languages code at a somewhat higher level. The general impression I get from people though is that the reason it is slower is that it uses JIT compilation. I can’t see why this isn’t a advantage, I mean if you first convert you code to MSIL then to platform specific binary code, shouldn’t you get just as good binary code as code compiled with a C++ compiler. When I think about it, I would also imagine you could get even faster than normally compiled code, by when compiling using processor specific instruction sets (SSE 1-3, 3DNow, MMX etc.) and optimize for the specific processor and other hardware, then when the hardware changes the MSIL code could be compiled again (it would of course be kept after the first compilation).So could anyone explain to me why .NET isn’t faster than normally compiled code, which can’t even make assumptions about the hardware.   
  While JIT compilation is a potentially great source of run-time optimization, even the academic world has yet to figure out a truly grweat way to do it.  Also, the JIT happens each time the code is run (as in, each time the process is launched), unless you n'gen, so you get slowdowns from the JITter itself.   ;Quote:Original post by CpManAlso, the JIT happens each time the code is run (as in, each time the process is launched), unless you n'gen, so you get slowdowns from the JITter itself.I heard that .NET languages only compiles on the first run, and next time the code is run the already compiled code will be reused. Isn't that correct?   ;  I think it all depends on how well the JIT compiler can take the interpreted code and compile it for the specific hardware the virtual machine is running on. I don't think at the stage the .net JIT compiler can compete with a standalone binary code compiler that is spceifically aimed to compile to a certain architecture. I beleive it's getting closer though.edit: Beaten by cpman. ASP.NET pages are compiled and saved the first time they are run but I thought .Net Winforms apps weren't (hence "Just-In-Time"), could be wrong though.   ;  The problem is that the JIT compiler has to be very fast, optimizing a C++ file can take quite a lot of time, this is usually not excepted for JIT compilation. As a result the compiler optimizes less. There is also a small overhead from .NET features like array bounds checking, and possibly garbage collection.   ;Quote:Original post by CTarQuote:Original post by CpManAlso, the JIT happens each time the code is run (as in, each time the process is launched), unless you n'gen, so you get slowdowns from the JITter itself.I heard that .NET languages only compiles on the first run, and next time the code is run the already compiled code will be reused. Isn't that correct? .NET does indeed cache the recompiled code.Anyway, the stuff you said is theoretically correct, but we haven't really gotten there yet. Of course, it also depends on the code. Axiom, the port of OGRE to C#, was actually faster than OGRE for a time (the project has unfortunately since fallen into disuse). They also did significant refactoring and such while porting, so it may not be due to C# as such. Who can say?   ;Quote:Original post by Neil KerkinI think it all depends on how well the JIT compiler can take the interpreted code and compile it for the specific hardware the virtual machine is running on.Just so we're clear, there is no virtual machine if using a JIT compiler.CM   ;Quote:Original post by PromitQuote:Original post by CTarQuote:Original post by CpManAlso, the JIT happens each time the code is run (as in, each time the process is launched), unless you n'gen, so you get slowdowns from the JITter itself.I heard that .NET languages only compiles on the first run, and next time the code is run the already compiled code will be reused. Isn't that correct? .NET does indeed cache the recompiled code.Anyway, the stuff you said is theoretically correct, but we haven't really gotten there yet. Of course, it also depends on the code. Axiom, the port of OGRE to C#, was actually faster than OGRE for a time (the project has unfortunately since fallen into disuse). They also did significant refactoring and such while porting, so it may not be due to C# as such. Who can say?It depends.  I know some stuff won't.  For instance, open up Fonts and Colors in VS.  The first time you do this, you'll see the JIT, and some lag.  After this, it's cached until you next run VS.   