Monday, April 26, 2010

C#: Generated Code

Another post just to point out a nice link:

This post is fairly old now (2003), but still provides lots of useful information. It provides a good overview of what things cost in C#, by looking at the generated assembly code.

Overall, there aren't any significant surpises. Things seem to be fairly similar to C++. Instance function calls, basic variable manipulation, and simple math code is cheap and generates pretty much one-to-one assembly much like you can imagine C++ code does. 

In the same way, virtual functions and delegates are slow to call and should be avoided in performance-critical code much like C++. Memory layout and class internals are also fairly unsuprising, and structured as you would imagine; vtables are where you expect them, etc.

If you are comfortable with writing C++ and being aware of what sort of assembly code is generated, then you should be able to write C# code that performs well and doesn't give any nasty suprises.

Given that 7 (!) years have passed since that post, the runtime and optimizations should be significantly improved, so everything will be much faster and your generated code should contain alot of the sort of optimizations you can get from modern C++ compilers.

No comments:

Post a Comment