PInvoke 或使用 /clr:pure 进行编译
我有一组 C++ 数值库,我想用 F# 或 IronPython 等解释性语言以交互方式调用它们。
所以我现在有两个选择:
在本机 DLL 中编译库并使用 PInvoke 调用其中的函数。
使用 Visual C++(/clr:pure 编译选项)将 C++ 库编译为 .Net dll。
1的优点是它非常快,但是其中有更多的工作,例如我不能PInvoke双指针(例如float **),我必须在C++库中编写另一个包装器以使接口对.Net友好。
2 的优点是我不需要知道 Mashaling 字符串、数组等。但是,.net dll 比本机 dll 慢。
在两者之间进行选择时还应考虑哪些其他因素?
I have a set of numerical libraries in C++ and I want to call them interactively in a interpretive language like F# or IronPython.
So I have two choices now:
Compile the library in native DLL and use PInvoke to call functions in it.
Compile the C++ library to .Net dll using visual c++ (/clr:pure compile option).
The advantage of 1 is that it is very fast, however there are more work in it, e.g. I cannot PInvoke double pointer (e.g. float **), I must write another wrapper in the C++ library to make the interface friendly to .Net.
The advantage of 2 is that I don't need to do know Mashaling strings, arrays, etc. However, the .net dll is slower compared to the native one.
What others factors should be considered when choosing between the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你测量过速度差异吗? .net 并不像人们想象的那么慢 - 一旦加载了 .net 应用程序,它通常会被抖动,并且运行速度几乎与将其编译为本机代码一样快。
另外...编组不是免费的。它往往涉及复制数据,尤其是在应用程序域之间。除非您的库用很少的数据来回传递来完成大量工作,否则整理数据的成本可能会抵消您通过使库成为本机而获得的任何速度提升。
只有一种万无一失的方法可以回答这个问题:创建一个测试程序来使用该库,就像您实际使用它一样,以两种方式编译这些内容,然后看看哪个对您来说更快。
Have you measured the difference in speed? .net isn't as slow as people think it is -- once a .net app is loaded, it's usually jitted and often runs nearly as fast as if you'd compiled it as native code.
Also...marshaling ain't free. It tends to involve copying the data around, especially between app domains. Unless your libraries do massive amounts of work with little data passed back and forth, the cost of marshaling the data may negate any speed boost you'd get by making the library native.
There's only one foolproof way to answer the question: create a test program to use the library somewhat as you'd actually use it, compile the stuff both ways, and see which one's faster for you.
根据我使用 C++/CLI 的经验,它的性能比 PInvoke 好得多。
我使用 C++/CLI 封装了很多 C 代码,并且效果很好。
它还具有其他优点:使用 C++ 的能力、创建调用 C 函数的特定 .NET 接口的能力,甚至可以轻松地在 .NET 类中保留分配的非托管内存。它还允许您在编写和运行单元测试时查看非托管代码内的代码覆盖率。
C++/CLI 唯一的问题是它有点难学。它是一种相当复杂的语言,因为它同时具有所有 C++ 功能和所有 .NET 功能。 Visual Studio 并不像对待 C# 那样友好地对待它,而且您也没有出色的工具 Resharper 可以使用。
如果您关心性能,我相信您会发现 C++/CLI 是正确的选择。
From my experience with C++/CLI, it performs much better than PInvoke.
I have wrapped a lot of C code using C++/CLI and it works great.
It also has other benefits: The ability to use C++, the ability to create specific .NET interfaces that call the C functions and even to easily keep allocated unmanaged memory in the .NET classes. It also allows you to see the code coverage inside the unmanaged code when you write and run unit tests.
The only problem with C++/CLI is that it is a bit difficult to learn. It's a pretty complicated language since it has both all of the C++ features and all of the .NET features. Visual Studio doesn't treat it that nice like it does with C# and you don't have great tools Resharper to work with.
If you care about performance, I'm positive you'll find C++/CLI the right choice.