# [.net] Cost of using unmanaged wrappers in C#?

This topic is 4434 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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%.

##### Share on other sites
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 :)

##### Share on other sites

[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.

Thank you all :D

##### Share on other sites
You'll also notice a huge difference in performance when changing from Debug mode to Release mode. More on that...

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 9
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632147
• Total Posts
3004445

×