I was wondering if the two following scenarios have the same performance impact on native C++ code (if there is any performance impact at all).
Let’s assume I have the function cpp_calc() that is doing some calculation stuff and is written in native C++. Also, there is cs_show_gui_stuff(), which is written in C#.
Now, which of the following scenarios will worsen the native c++ performance (if there is any performance penalty at all)?
-
Creating a .Net (C#) application that runs
cs_show_gui_stuff()and callscpp_calc()in the native C++ dll usingDllImportor turning C++ into a COM DLL. -
Creating a C++ application that implements
cpp_calc()in C++ and runscs_show_guid_stuff()by placing the C# code in a .Net COM DLL.
Thanks 🙂
It really depends on what the other parts of the system are mainly written in. From a performance-only perspective, one
PInvoke(viaDllImportattribute) 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.