从托管代码调用非托管代码的性能 [英] Performance of invoking unmanaged code from managed code

查看:188
本文介绍了从托管代码调用非托管代码的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Win32 DLL和它使用同一个DLL的两个应用程序。一种是使用非托管空间C写的,另一种是在.NET应用程序,这使得呼叫使用Pinvokes的非托管代码。

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.

有时.NET应用程序的性能比C应用程序更好。从技术上讲,从.NET调用非托管代码会带来性能开销,但不是在这种情况下。

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.

我查了一下MSDN参考,并似乎有某种预处理前实际发生这种情况调用。在联动的dll被加载。

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 is loaded.

我要在.NET应用程序优于什么场景理解?

I want to understand in what scenarios .net application outperforms?

推荐答案

的PInvoke的编组是代码在.NET框架中最重的优化块之一。很重要的,有很多的PInvoke的非托管的操作系统上运行的管理程序,当你无法看到。开销的量是非常可变的。它不过是CPU周期一把当DLL已经加载和参数的pinvoked函数是简单的整数类型。到数百个周期的时候,说,字符串转换是必要的。

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.

在任何情况下是它曾经比本地调用更快。测量开销,这只是周期极少数准确是很难的。而且要记住,你可能会无意中衡量你的测试程序,而不是PInvoke的通话PERF。

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.

这篇关于从托管代码调用非托管代码的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆