How should I go about evaluating the performance or execution time of C++ with CLR versus native C++?
My personal bias is that I believe C++ with CLR to be slower since it has to run on a virtual machine. I will also admit that I have never really been a fan of Visual C++ due to the syntax that has been added, so I wanted to know how to evaluate the performance of the two approaches and determine which is provably faster instead of relying only on my biases.
1
I did some benchmarks in the past on my own, and my experience was that native C++ was around a factor 2 faster than C++/CLI for my typical business case, without any relevant change in code – your mileage will vary. For me, a factor 2 was almost always fully acceptable to stick to C++/CLI side, because interaction with the .NET framework and other .NET code was much more important for me than this performance gain.
0
Well, as always these kind of questions can’t be answered with an absolute yes or no.
My experiences in general is that C++/CLI performs fast enough for me to work with it in desktop applications, C library wrappers or similar. However, if I needed to build a large hard real time system, I would not use it.
You need to look at what you need to use it for and if it is really performance critical, you should build a prototype – or model or whatever you want to call it – and benchmark. Do you have a lot of disc IO? Do you need a lot of varying memory allocation? Do you have any real time constraints?
Just a clarification. Question is ambiguous which can mislead others.
C++/CLI Project you have choice to #managed portion as well #unmanaged portion.
in #managed portion you use a C++ version which is called C++/CLI or (C++ .NET) and is a special syntax containing handles instead of pointers or refs.
Yes this special syntax C++/CLI or C++.net in #managed portion will be half the speed of actual C++ because it eventually runs on virtual machine and is actually like C# code that runs on CLR.
But you usually don’t put your main C++ code in #managed portion basically you create this C++/CLI project to host mixed code. so you will put your C++ portions in #unmanaged portion and only the code that does interoperability between the two will go to #managed portion. classes declared with ref class are managed in same project while normal classes are unmanaged.
Unmanaged code will be compiled as pure native code exactly as normal C++ code. Only the calls to this code from #managed portion will include thunks so that’s the only slowing part, the rest runs 100% as native c++ code.
Hope this clearifies.