的P / Invoke和C ++包装的性能差异 [英] Performance differences between P/Invoke and C++ Wrappers

查看:168
本文介绍了的P / Invoke和C ++包装的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习的P / Invoke的过程中,我问这个问题之前的:

In the process of learning P/Invoke, I asked this previous question:

如何的p / Invoke时,指针都参与

不过,我不明白了在托管C创建一个包装++在C#中使用p / Invoke的影响。创建,使用P同一个DLL / Invoke的在C#中肯定产生了一个简洁的界面,因为我可以在嵌入的资源使用的DllImport,而是将对于本机DLL,在那里我做了封送自己一个托管C ++包装,有更好的表现?

However, I don't quite understand the implications of using P/Invoke in C# over creating a wrapper in Managed C++. Creating the same DLL using P/Invoke in C# definately resulted in a cleaner interface since I could use DLLImport on an embedded resource, but would a Managed C++ wrapper for a native DLL, where I do the marshaling myself, have better performance?

推荐答案

C ++包装要快,看看这个的 MSDN页面

C++ wrapper should be faster, have a look at this MSDN page:

C ++的Interop使用数据编组的最快的方法,而的P / Invoke采用最可靠的方法。这意味着,C ++的Interop(典型的对C时尚++)默认情况下提供最佳的性能,而程序员负责解决情况下,这种行为是不是安全或适当。

C++ Interop uses the fastest possible method of data marshaling, whereas P/Invoke uses the most robust method. This means that C++ Interop (in a fashion typical for C++) provides optimal performance by default, and the programmer is responsible for addressing cases where this behavior is not safe or appropriate.

所以基本上主要的原因是的P / Invoke并钉住,块传输,错误检查,而C ++的互操作只是推栈上的参数并调用该函数。

So basically the main reason is that P/Invoke does pinning, blitting, error checking, while C++ interop just pushes the parameters on the stack and calls the function.

另外要记住的一点是,C ++可以调用几个API在一个单一的通话时的P / Invoke按地址传递每一个参数被固定和取消固定在每次调用,复制
和复制回等。

Another point to remember is that C++ can call a several APIs in a single call while P/Invoke EVERY parameter passed by address gets pinned and unpinned on EVERY call, copied and copied back, etc.

这篇关于的P / Invoke和C ++包装的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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