.net 中使用的所有语言都具有相同的性能吗?

发布于 2024-08-28 09:45:13 字数 266 浏览 10 评论 0原文

我知道这个问题的“推销”答案是肯定的,但从技术上讲这是真的吗?

公共语言运行时 (CLR) 被设计为基于命令式编程 (IP) 的中间语言,但这在处理声明式编程 (DP) 时具有明显的影响。

那么,在 CLR 中实现时,基于与命令式风格不同范式的语言的效率如何?

我还感觉 DP 的步骤会带来额外的抽象级别,可能根本无法建模,这是一个公平的评论吗?

我已经使用 F# 完成了一些简单的测试,一切看起来都很棒,但是如果程序变得更加复杂,我是否会错过一些东西?

I know the "Sales pitch" answer is yes to this question, but is it technically true.

The Common Language Runtime (CLR) is designed as an intermediate language based on Imperative Programming (IP), but this has obvious implications when dealing with Declarative Programming (DP).

So how efficient is a language based on a different paradigm than the Imperative Style when implemented in the CLR?

I also get the feeling that the step to DP would incur an extra level of abstraction that might not model at all performant, would this be a fair comment?

I have done some simple tests using F# and it all looks great, but am I missing something if the programs get more complex?

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

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

发布评论

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

评论(5

后来的我们 2024-09-04 09:45:14

归根结底,所有编程语言都被编译成它们所运行的 CPU 的本机机器代码,因此任何语言都可以提出同样的问题(不仅仅是那些编译为 MSIL)。

对于本质上只是彼此语法变体的语言(例如 C# 与 VB.NET),我不认为会有太大差异。但是,如果语言差异太大(例如 C# 与 F#),那么您无法真正进行有效的比较,因为您无法真正用两种语言编写两个“等效”非平凡代码示例反正。

At the end of the day, all programming languages are compiled into the native machine code of the CPU they're running on, so the same questions could be asked of any language at all (not just ones that compile to MSIL).

For languages that are essentially just syntatic variants of each other (e.g. C# vs. VB.NET) then I wouldn't expect there to be much difference. But if the languages are too divergent (e.g. C# vs. F#) then you can't really make a valid comparison because you can't really write two "equivalent" non-trivial code samples in both languages anyway.

一人独醉 2024-09-04 09:45:14

该语言可以被视为 IL 代码的“前端”,因此语言之间的唯一区别在于编译器是否会生成相同的 IL 代码或效率较低/较高的代码。

从我在网上阅读的大部分内容来看,托管 C++ 编译器似乎在优化 IL 代码方面做得最好,尽管我还没有看到任何显示主要语言 C#/C++/VB.NET 之间存在显着差异的内容。 。

你甚至可以尝试将以下内容编译成 IL 来看看!?

F#

#light
open System
printfn "Hello, World!\n"
Console.ReadKey(true)

C#

// Hello1.cs
public class Hello1
{
    public static void Main()
    {
        System.Console.WriteLine("Hello, World!");
        System.Console.ReadKey(true);
    }
}

The language can just be thought of as the "front-end" to the IL code, so the only difference between the languages is whether the compiler will produce the same IL code or less/more efficient code.

From most of what I've read online it seems as though the managed C++ compiler does the best job of optimizing the IL code, although I haven't seen anything that shows a remarkable difference between the main languages C#/C++/VB.NET.

You could even try compiling the following into IL and take a look!?

F#

#light
open System
printfn "Hello, World!\n"
Console.ReadKey(true)

C#

// Hello1.cs
public class Hello1
{
    public static void Main()
    {
        System.Console.WriteLine("Hello, World!");
        System.Console.ReadKey(true);
    }
}
椒妓 2024-09-04 09:45:13

无法保证语言为等效代码生成相同的 IL,因此我可以有把握地说,无法保证所有 .NET 语言都具有相同的性能。

然而,如果它们产生相同的 IL 输出,则没有区别。

There is no guarantee that languages produce the same IL for equivalent code, so I can safely say that there is no guarantee that all .NET languages are equally performant.

However, if they produce the same IL output, then there is no difference.

柠檬心 2024-09-04 09:45:13

首先,.NET 平台上的各种语言肯定包含生成具有不同性能的代码的语言,因此并非所有语言都具有相同的性能。它们都编译为相同的中间语言(IL),但生成的代码可能不同,某些语言可能依赖于反射或动态语言运行时(DLR)等。

但是,确实,BCL(以及BCL使用的其他库)语言)将具有相同的性能,无论您从什么语言调用它们 - 这意味着,如果您使用一些进行昂贵计算或渲染的库,而无需自己进行复杂计算,那么使用哪种语言调用它并不重要。

我认为思考这个问题的最好方法不是考虑语言,而是考虑这些语言中可用的不同功能和编程风格。下面列出了其中的一些内容:

  • 不安全代码:您可以在 C++/CLI 中使用不安全代码,在某种程度上也可以在 C# 中使用不安全代码。这可能是编写某些操作的最有效方法,但会失去一些安全保证。

  • 静态类型、命令式:这是 C# 和 VB.Net 中的常见编程风格,但您也可以使用 F# 中的命令式风格。值得注意的是,许多尾递归函数被编译为静态类型、命令式 IL 代码,因此这也适用于某些 F# 函数

  • 静态类型、函数式:大多数 F# 程序都使用它。生成的代码与命令类别使用的代码有很大不同,但它仍然是静态类型的,因此没有显着的性能损失。比较命令式和功能性有点困难,因为两个版本中的最佳实现看起来完全不同。

  • 动态类型:像 IronPython 和 IronRuby 这样的语言使用动态语言运行时,它实现动态方法调用等。这比静态类型代码要慢一些(但 DLR 在很多方面进行了优化)。请注意,使用 C# 4.0 dynamic 编写的代码也属于此类。

还有许多其他语言可能不属于这些类别中的任何一个,但是我相信上面的列表涵盖了大多数常见情况(并且肯定涵盖了所有 Microsoft 语言)。

First of all, the wide range of languages on the .NET platform definitely contains languages that generate code with different performance, so not all languages are equally performant. They all compile to the same intermediate language (IL), but the generated code may be different, some languages may rely on Reflection or dynamic language runtime (DLR) etc.

However, it is true that the BCL (and other libraries used by the languages) will have the same performance regardless of what language do you call them from - this means that if you use some library that does expensive calculations or rendering without doing complex calculations yourself, it doesn't really matter which language you use to call it.

I think the best way to think about the problem is not to think about languages, but about different features and styles of programming available in those languages. The following lists some of them:

  • Unsafe code: You can use unsafe code in C++/CLI and to some point also in C#. This is probably the most efficent way to write certain operations, but you loose some safety guarantees.

  • Statically typed, imperative: This is the usual style of programming in C# and VB.Net, but you can also use imperative style from F#. Notably, many tail-recursive functions are compiled to statically typed, imperative IL code, so this also applies to some F# functions

  • Statically typed, functional: This is used by most F# programs. The generated code is largely different than what imperative category uses, but it is still statically typed, so there is no significant performance loss. Comparing imperative and functional is somewhat difficult as the optimal implementation looks quite different in both of the versions.

  • Dynamically typed: Languages like IronPython and IronRuby use dynamic language runtime, which implements dynamic method calls etc. This is somewhat slower than statically typed code (but DLR is optimized in many ways). Note that code written using C# 4.0 dynamic also falls into this category.

There are many other languages that may not fall into any of these categoires, however I believe that the above list covers most of the common cases (and definitely covers all Microsoft languages).

走过海棠暮 2024-09-04 09:45:13

我确信在某些情况下,用一种 .NET 语言编写的惯用代码的性能会比用另一种 .NET 语言编写的性能稍高一些。然而,退一步来说,为什么这很重要呢?您心中有绩效目标吗?即使在单一语言中,您也经常可以做出影响性能的选择,有时您需要在性能与可维护性或开发时间之间进行权衡。如果您没有构成可接受性能的目标,则无法评估语言之间的任何性能差异是否有意义或可以忽略不计。

此外,编译器不断发展,因此今天的相对性能不一定适用于未来。 JIT 编译器也在不断发展。即使处理器设计也是可变且不断发展的,因此相同的 JITTed 本机代码可以在具有不同缓存层次结构、管道大小、分支预测等的处理器上以不同的方式执行。

话虽如此,可能有一些在很大程度上适用的广泛规则:

  1. 算法差异可能会比编译器差异产生更大的差异(至少在比较在 CLR 上运行的静态类型语言时)
  2. 对于可以轻松并行化的问题,可以轻松利用多个处理器/内核的语言将提供加速代码的简单方法。

I'm sure that there are scenarios where idiomatic code is slightly more performant when written in one .NET language than another. However, stepping back a bit, why does it matter? Do you have a performance target in mind? Even within a single language there are often choices which you can make that affect performance, and you will sometimes need to trade performance off against maintainability or development time. If you don't have a target for what constitutes acceptable performance, then it's impossible to evaluate whether any performance differences between languages are meaningful or negligible.

Additionally, compilers evolve, so what's true of the relative performance today won't necessarily hold going forward. And the JIT compiler is evolving too. Even processor designs are variable and evolving, so the same JITTed native code can perform differently across processors with different cache hierarchies, pipeline sizes, branch prediction, etc.

Having said all of that, there are probably a few broad rules that largely hold true:

  1. Algorithm differences are probably going to make a bigger difference than compiler differences (at least when comparing statically typed languages running on the CLR)
  2. For problems which can be parallelized easily, languages which make it easy to take advantage of multiple processors/cores will provide a simple way to speed up your code.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文