Coffeehouse Post

Single Post Permalink

View Thread: is managed code faster than native?
  • User profile image
    Sven Groot

    , SteveRichter wrote

    the C# compiler is like 5 - 10x faster than the c++ compiler. Does that prove that real world managed code runs faster than native code?

    There are several reasons for this. Most importantly, C++ is a far more complex language (grammar wise) than C# so it takes longer to parse (particularly thanks to templates, which are a Turing-complete compile-time language), and the C++ compiler performs far more extensive optimizations (in the .Net world, most optimizations are performed by the JIT, so the work of the C# compiler is relatively light).

    Whether managed or native is faster has no simple answer. With native code, it is possible to perform more optimizations so it is possible to write faster code than with managed. However, in practice it may be require extremely complex and hard to maintain code to actually beat managed code. A good example of this is the optimization series that Raymond Chen and Rico Mariani once did: his final optimized C++ version was faster than the .Net version, but he had to write his own memory allocation algorithms and stuff like that in order to get there. By comparison, the naïve .Net version was orders of magnitude faster than the naïve C++ version, and not even that much slower than the final optimized C++ version.

    And that was done back in 2005, with .Net 2.0. There have been many significant performance improvements in .Net (particularly regarding start-up and the GC) since then.