[.net] Question about managed vs. unmanaged code

Started by
1 comment, last by BradSnobar 18 years, 5 months ago
So, let's say I'm writing my project primarily in C# but I am going to use some native C or C++ for a few performance reasons. How is this unmanaged code really handled relative to the MSIL of the managed code, the JIT compiler and garbage collection? Also, are there any resources net or bookwise that address this specific issue in detail?
Advertisement
Quote:Original post by Ned_K
So, let's say I'm writing my project primarily in C# but I am going to use some native C or C++ for a few performance reasons. How is this unmanaged code really handled relative to the MSIL of the managed code, the JIT compiler and garbage collection? Also, are there any resources net or bookwise that address this specific issue in detail?

Taking it a line at a time:

I'd question the statement "for a few performance reasons". Unless you have two different versions and have measured exactly why one is much faster, you are just guessing and usually wrong.

Unmanaged code is able to use pointers and directly manage memory. The actual code will be handled differently based on the security settings and policies. It will make no significant difference with the JIT compiler. Garbage collection is also pretty much unchanged, you just need to be careful with properly freeing memory of your unmanaged code.

There are lots of books, but you might not need them if you are already comfortable with C++ windows programming.

One easy option is to used managed C++. If you are comfortable with the language already then you should be able to pick up the differences from MSDN and compiler warnings. Otherwise you might want to go browse the bookstore.

Another easy option is to use the C++ to generate a DLL and then use P/Invoke from the C# code.

If you're going the other way, make the C# code into a DLL and call it from the C++ code.

frob.
I just ran a benchmark comparison against Native C++ and C# (.Net 2.0) yesterday.

While the diffence is noticable on .Net 1.0, I got almost identical results on the new version of .Net. Pretty amazing if you ask me.

The total operations (math, IO, etc, etc) took about 42 seconds on c++ and about 43 on C#.

The difference used to be about 10 seconds.

This topic is closed to new replies.

Advertisement