[.net] Cost of using unmanaged wrappers in C#?
How much of a performance hit % is it to use a wrapper to access C++ functions/classes from C#?
Context: I was thinking of using the OgreDotNet wrapper for Ogre3D, and just wanted to find out this small point. I know speed for small games is irrelevant, but I'd just like to know.
Most of the cost is in marshalling/unmarshalling the data. For instance, strings must be made into a form that is usable in a C/C++ API. I don't think its possible to come up with a % in general, because it depends greatly upon how often you cross the boundary between managed and unmanaged code, and the kind/amount of data you have to marshal/unmarshal. But, my experience is that the performance hit is usually minor. It depends upon how efficient your wrappers are, but my guess is that the performance hit caused by the wrapper will be significantly less than the overall performance hit of using C# over C++, which many gauge at about 10%.
While this isn't exactly the answer your looking for, it should be more or less close to the same thing. Axiom, the C# version of OGRE, runs at about the same speed as OGRE does. Some cases it was faster, some it was slower. And while Axiom was written in C#, it still has to access the unmanaged libraries for all of its 3D functionality.
I hope that helps :)
I hope that helps :)
make sure you add the following attribute to your .dll imports:
This way, the runtime will not do a stack walk of (which is checking that no local memory has been corrupted in the unmanaged function). This assumes that you *know* that the unmanaged function is rock solid. Naturally it makes things much quicker, but has a certain risk.
[System.Security.SuppressUnmanagedCodeSecurity][DllImport(...
This way, the runtime will not do a stack walk of (which is checking that no local memory has been corrupted in the unmanaged function). This assumes that you *know* that the unmanaged function is rock solid. Naturally it makes things much quicker, but has a certain risk.
You'll also notice a huge difference in performance when changing from Debug mode to Release mode. More on that...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement