2

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)?

  1. Creating a .Net (C#) application that runs cs_show_gui_stuff() and calls cpp_calc() in the native C++ dll using DllImport or turning C++ into a COM DLL.

  2. Creating a C++ application that implements cpp_calc() in C++ and runs cs_show_guid_stuff() by placing the C# code in a .Net COM DLL.

Thanks :-)

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
TCS
  • 5,790
  • 5
  • 54
  • 86
  • this question is somewhat a little bit related to performance of native code. hope it helps, though not answering yours: http://stackoverflow.com/questions/5693477/ccli-does-the-native-parts-written-in-pure-c-but-compiled-in-cli-are-as-fast – Stephane Rolland May 27 '11 at 07:59
  • 7
    Before trying to figure that out, did you actually test that the calculation function does not have acceptable performance in C#? Premature optimization yadda yadda... you all know the text :)= – Medo42 May 27 '11 at 08:00
  • @Medo42 - Its more of a theoretical question. I sometimes find myself having that question in mind in various scenarios. If someone can give a complete answer to that question we won't need to test (in simple cases).And besides that, we will all learn something :-) . – TCS May 27 '11 at 08:38

2 Answers2

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.

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
Florian Greinacher
  • 14,478
  • 1
  • 35
  • 53
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.

Wojtek Turowicz
  • 4,086
  • 3
  • 27
  • 38