简体   繁体   中英

Is C++/CLI faster than C#

Is C++/CLI faster than C#? In which type of operations is it faster?

Not necessarily. However, C++/CLI takes away much of the syntactic sugar around non-performant ways of doing things that is present in C# (boxing for example).

Also, C++/CLI allows you a much more clean interop with unmanaged code, actually allowing you to mix managed / unmanaged code, which is a performance crucial environment may be of benifit.

EDIT:

See this post for some of the differences: http://msdn.microsoft.com/en-us/library/ms379617(VS.80).aspx

Since they both run on the .NET framework, I'd say any performance difference would be negligable. Any difference will almost certainly be down to how well whichever compilers you are using work.

Well, the short answer is no . Why? Reference types in C++/CLI are compiled to MSIL, same as in C#.

The nice thing about C++/CLI (and the long answer) though, is that you can easily call into native code, which (in many cases) is faster. That being said, if you write a native C++ class and expect it to be executed natively when called by someone in a managed class, that native C++ class must be compiled without CLR support ( this question goes into how to do that).

Any managed code written in C++/CLI will essentially be exactly the same as the equivalent C#, assuming compiler accuracy, as they'll both end up as intermediate language instructions. However, C++/CLI makes it easy to mix unmanaged code in with the managed portion which may provide considerable speed benefits if well optimised.

Seeing as they are both .NET languages that get compiled into the same byte code that in turn gets run on the same virtual machine I'd say in general, no.

C++/CLI is really only intended to provide language interop between .NET and C++.

Yes, because except few details you can use .net like any other lib while you continue to have the power of c++. Mixing c++ classes with .net classes, inline assembly, create a driver if you want, ect. In c# you can use usafe code but you can't access direct2d api for example, it is limited.

Direct2dTest::MyForm^ form = gcnew Direct2dTest::MyForm();

//use (HWND)form.Handle.ToPointer() to acces the hwnd;

form->Show();
//you can use 
//System::Windows::Forms::Application::Run(form); 
//or a classic win32 loop
while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM