Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++/CLI performance compared to Native C++?

Good morning,

I am writting a spell checker which, for the case, is performance-critical. That being, and since I am planning to connect to a DB and making the GUI using C#, I wrote an edit-distance calculation routine in C and compiled to a DLL which I use in C# using DllImport. The problem is that I think (though I am possibly wrong) that marshalling words one by one from String to char * is causing a lot of overhead. That being, I thought about using C++/CLI so that I can work with the String type in .NET directly... My question is then how does C++/CLI performance compares to native C code for heavy mathematical calculations and array access?

Thank you very much.

like image 905
Miguel Avatar asked Dec 06 '10 10:12

Miguel


People also ask

Is C++ CLI faster than C#?

Performance: C++ is widely used when higher level languages are not efficient. C++ code is much faster than C# code, which makes it a better solution for applications where performance is important.

Is C++ good for CLI?

C++ is by far the best language to develop Linux CLIs.


3 Answers

C++/CLI will have to do some kind of marshaling too.

Like all performance related problems, you should measure and optimize. Are you sure C# is not going to be fast enough for your purposes? Don't underestimate the optimizations that JIT compiler is going to do. Don't speculate on the overhead of a language implementation solely for being managed without trying. If it's not enough, have you considered unsafe C# code (with pointers) before trying unmanaged code?

Regarding the performance profile of C++/CLI, it really depends on the way it's used. If you compile to managed code (CIL) with (/clr:pure), it's not going to be very different from C#. Native C++ functions in C++/CLI will have similar performance characteristics to plain C++. Passing objects between native C++ and CLI environment will have some overhead.

like image 181
mmx Avatar answered Sep 23 '22 19:09

mmx


I would not expect that the bottleneck will be with the DLLImport.
I have written programs which call DLLImport several hundert times per second and it just works fine.
You will pay a small performance fine, but the fine is small.

like image 39
weismat Avatar answered Sep 20 '22 19:09

weismat


Don't assume you know what needs to be optimized. Let sampling tell you.

I've done a couple spelling-correctors, and the way I did it (outlined here) was to organize the dictionary as a trie in memory, and search upon that. If the number of words is large, the size of the trie can be much reduced by sharing common suffixes.

like image 23
Mike Dunlavey Avatar answered Sep 21 '22 19:09

Mike Dunlavey