C++/CLI 比 C# 更快吗

发布于 2024-08-02 08:38:43 字数 38 浏览 6 评论 0原文

C++/CLI 比 C# 更快吗?在哪种类型的操作中速度更快?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ゞ花落谁相伴 2024-08-09 08:38:43

未必。然而,C++/CLI 消除了 C# 中存在的非性能操作方式的大部分语法糖(例如拳击)。

此外,C++/CLI 允许您与非托管代码进行更干净的互操作,实际上允许您混合托管/非托管代码,这对于性能至关重要的环境可能是有益的。

编辑:

请参阅这篇文章了解一些差异: http:// /msdn.microsoft.com/en-us/library/ms379617(VS.80).aspx

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

栖竹 2024-08-09 08:38:43

由于它们都在 .NET 框架上运行,因此我认为任何性能差异都可以忽略不计。几乎可以肯定,任何差异都取决于您使用的编译器的工作效果。

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.

荒路情人 2024-08-09 08:38:43

嗯,简短的回答是。为什么? C++/CLI 中的引用类型被编译为 MSIL,与 C# 中相同。

不过,C++/CLI(以及长答案)的好处是,您可以轻松调用本机代码,这(在许多情况下)速度更快。话虽这么说,如果您编写一个本机 C++ 类并希望它在托管类中的某个人调用时能够本机执行,则该本机 C++ 类必须在没有 CLR 支持的情况下进行编译 (这个问题涉及如何做到这一点)。

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

百善笑为先 2024-08-09 08:38:43

假设编译器的准确性,用 C++/CLI 编写的任何托管代码本质上都与等效的 C# 完全相同,因为它们最终都会成为中间语言指令。然而,C++/CLI 可以轻松地将非托管代码与托管部分混合在一起,如果优化得当,可以提供相当大的速度优势。

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.

软的没边 2024-08-09 08:38:43

鉴于它们都是 .NET 语言,都被编译成相同的字节代码,而这些字节代码又在同一个虚拟机上运行,​​所以我一般会说,不。

C++/CLI 实际上只是为了提供 .NET 和 C++ 之间的语言互操作。

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++.

悲念泪 2024-08-09 08:38:43

是的,因为除了一些细节之外,您可以像任何其他库一样使用 .net,同时继续拥有 C++ 的强大功能。将 c++ 类与 .net 类混合、内联汇编、根据需要创建驱动程序等。在 C# 中,您可以使用 usafe 代码,但无法访问 direct2d api,例如,它是有限的。

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文