2

我想知道以下两种情况是否对本机 C++ 代码具有相同的性能影响(如果有任何性能影响的话)。

假设我有一个cpp_calc()正在做一些计算的函数,并且是用原生 C++ 编写的。此外,还有cs_show_gui_stuff(),它是用 C# 编写的。

现在,以下哪种情况会恶化本机 c++ 性能(如果有任何性能损失的话)?

  1. 创建一个在本地 C++ dll中运行cs_show_gui_stuff()和调用的 .Net (C#) 应用程序,使用或将 C++ 转换为 COM DLL。cpp_calc()DllImport

  2. 通过将 C# 代码放置在 .Net COM DLL 中来创建在 C++中实现cpp_calc()并运行的 C++ 应用程序。cs_show_guid_stuff()

谢谢 :-)

4

2 回答 2

9

It really depends on what the other parts of the system are mainly written in. From a performance-only perspective, one PInvoke (via DllImport attribute) call will probably be faster than one COM call if the method arguments do not need any special marshaling.

A third, and probably the best alternative, is to create a managed C++/CLI library that calls the unmanaged C++ method with nearly no performance impact and add a reference to the C++/CLI library in the C# application. The C# application can then make managed method calls to the C++/CLI application, which in turn can make unmanaged method calls. While this adds one level of indirection, it will provide way better performance than the methods you mentioned.

于 2011-05-27T07:59:54.953 回答
1

Either way You are going to bump on Just-In-Time compiler. I guess the penalty is the same on both scenarios. I would personally choose the first one, because .NET libs are more robust on the GUI - WPF, Silverlight, WinForms, WebForms, Razor ... You see what I mean.

于 2011-05-27T07:58:40.273 回答