C# Program Optimisation-Collection of common programming errors


  • msdn Hi,I am developing a very resource-hungry application that runs more slowly than I would like. I have heard that C# performs many checks at runtime that use up many resources (I think it was Wikipedia) – is it possible to turn those off as surley I would only need them while debugging?
  • 8 Answers


  • msdn1 The C# compiler relies upon the JIT to do most of the optimization.  When you “build” your program in Visual Studio, it does little more than convert the C# code to an MSIL program.The JIT, which turns the MSIL into machine-language code that your computer actually uses, is not optimized for long-running processes.  It’s designed for fast compilation, rather than quality compilation.  This is because the traditional .NET app is a simple UI application where the delays associated with compilation might cause an undesirable lag for the user.You can use the NGEN utility to pre-compile your application for the CPU.  NGEN doesn’t face the same compile time constraints as the JIT assumes, so it can theoretically take all the time needed to produce a faster program.  Unfortunately, as of today, NGEN doesn’t really do much optimization either (at least, not as much as it could).C++ compilers almost always produce faster programs because they’ve been tuned and enhanced for many years.-Ryan / Kardax

  • msdn2 Why is C++ faster than C#?Surley all the processing should be done at compile-time.

  • msdn3 The C# compiler relies upon the JIT to do most of the optimization.  When you “build” your program in Visual Studio, it does little more than convert the C# code to an MSIL program.The JIT, which turns the MSIL into machine-language code that your computer actually uses, is not optimized for long-running processes.  It’s designed for fast compilation, rather than quality compilation.  This is because the traditional .NET app is a simple UI application where the delays associated with compilation might cause an undesirable lag for the user.You can use the NGEN utility to pre-compile your application for the CPU.  NGEN doesn’t face the same compile time constraints as the JIT assumes, so it can theoretically take all the time needed to produce a faster program.  Unfortunately, as of today, NGEN doesn’t really do much optimization either (at least, not as much as it could).C++ compilers almost always produce faster programs because they’ve been tuned and enhanced for many years.-Ryan / Kardax

  • msdn4

    Do you know of any good programs to convert C# to C++?


  • msdn5 It’s not really possible to convert a whole C# project to C++ automatically.  A major integral feature of C# is a garbage collector; C++ does not natively have this, so a conversion would be required to figure out all the object lifetimes.  A human could do that (laboriously), but an automated system could not.

    With proper design, it’s possible to get most computationally-intensive C# applications to within 80-85% of C++ performance.  Given how much work would be required to convert from C# to C++ for such a small gain, it’s probably easier to buy a faster computer

    -Ryan / Kardax


  • msdn6 Would it be possible to turn off garbage collection?Also, why isn’t garbage collection done at compile-time? Surley that’s why we have to enter a static variable name.

  • msdn7 If garbage collection were disabled, there would be no way to release the memory used by objects.  C# has no “delete” command.  This feature is thus required, and always on.Garbage collection can’t be done only at compile time because new object instances can be created at run time (and always are in non-trival programs).For a well-designed program, the garbage collector has no effect on performance.  A program that does not do any memory allocations during its heavy processing phases will not trigger any garbage collection activity.-Ryan / Kardax

  • msdn8

    C# 3.0 lets you dump variables.