Performance of invoking unmanaged code from managed code
I have a win32 dll and two applications which uses the same dll. One is written using c in unmanaged space and the other is the .net application which makes call to the unmanaged code using Pinvokes.
Sometimes the performance of the .net application is better than the c application. Technically speaking invoking unmanaged code from .net incurs performance overhead but not in this case.
I checked the msdn reference and there seems to be some kind of preprocessing that happens before actual call is made. During linkage the dll i开发者_JAVA百科s loaded.
I want to understand in what scenarios .net application outperforms?
The pinvoke marshaller is one of the heaviest optimized chunks of code in the .NET framework. Very important, there's lots of pinvoke you cannot see when running a managed program on an unmanaged operating system. The amount of overhead is highly variable. It is but a handful of cpu cycles when the DLL is already loaded and the arguments to the pinvoked function are simple integral types. To many hundreds of cycles when, say, string conversions are necessary.
In no circumstance is it ever faster than a native call. Measuring overhead that's only a handful of cycles accurately is difficult. And keep in mind that you might unintentionally measure the perf of your test program instead of the pinvoke call.
精彩评论