简体   繁体   English

F#和C#的CLR相同,那么为什么F#比C#快

[英]F# and C# 's CLR is same then why is F# faster than C#

I am confused and would appreciate if you enlighten me. 我很困惑,如果您能启发我,我将不胜感激。 F# uses same CLR as C# and underlying code is identical then how can one suggest that a function runs faster when written in F# than C#? F#使用与C#相同的CLR,并且底层代码相同,那么如何建议用F#编写的函数比C#更快地运行? If I use only immutable variables in C# and performance needs to be as high as possible then why use F#? 如果我在C#中仅使用不可变变量,并且性能需要尽可能高,那么为什么要使用F#?

underlying code is identical? 底层代码是否相同? Doubtful. 疑。 In general, F# will be faster in some things and slower in others. 通常,F#在某些情况下会更快,而在另一些情况下会更慢。 The same is true of C#. C#也是如此。 Or even VB. 甚至VB。 Each language has its pluses. 每种语言都有其优点。 If there is an overall performance plus in most areas, it is in the compiler. 如果在大多数方面都具有整体性能,那么它在编译器中。

If I use only immutable variables in C# and performance needs to be as high as possible then why use F#? 如果我在C#中仅使用不可变变量,并且性能需要尽可能高,那么为什么要使用F#? No reason to switch for performance alone, if in fact there is a real difference, unless perf is your problem. 如果实际上存在实际差异,那么没有理由单独切换性能,除非性能问题出在您身上。 As far as why switch if you are only using features that are fine in C#, I would say "don't switch". 至于为什么要切换(如果您仅使用在C#中可以使用的功能),我会说“不要切换”。

I like F#. 我喜欢F#。 There are some "problems" it solves much better than C#. 有一些“问题”比C#解决的要好得多。 But, even if I have a problem it solves better, I am not necessarily switching, as I have to consider the developers in the mix. 但是,即使我有一个可以更好地解决的问题,也不一定要切换,因为我必须考虑各种开发人员。 Currently, I know of few in this organization that know anything about F#, so I would have to make a good business case to switch. 目前,我在这个组织中鲜有了解F#的人,因此我必须提出一个很好的业务案例来进行转换。

Ultimately, you have to look at the "business problem" and determine a path. 最终,您必须查看“业务问题”并确定路径。 You have to consider the "better" language as part of the mix, but you also have to examine core competency, etc. 您必须将“更好”的语言视为混合语言的一部分,但还必须检查核心能力等。

1) If you write C# code and F# code which gets compiled to exactly the same IL (the CLR's intermediate language), then the code will exhibit the same performance characteristics. 1)如果编写的C#代码和F#代码被编译为完全相同的IL(CLR的中间语言),则该代码将表现出相同的性能特征。 However, implementing the same algorithm in C# and F# does not guarantee that exactly the same IL will be generated, so real world performance may vary. 但是,在C#和F#中实现相同的算法并不能保证将生成完全相同的IL,因此实际性能可能会有所不同。 I believe that in practice sometimes F# code will be faster but other times C# will be faster. 我相信实际上有时F#代码会更快,而其他时候C#会更快。

2) There are many reasons to choose an implementation language besides performance. 2)除了性能之外,还有很多原因选择执行语言。 For example, some people find F# to be more succinct, readable, and easier to reason about than C# for certain problem domains, which would be one reason to prefer it. 例如,对于某些问题域,有些人发现F#比C#更加简洁,易读且易于推理,这是选择它的原因之一。 For the vast majority of programs, a 5% or 10% difference in performance is going to be imperceptible to users, so if a language offers roughly comparable performance but superior productivity then it would make sense to use that language. 对于绝大多数程序而言,用户不会察觉到5%或10%的性能差异,因此,如果一种语言可提供大致可比的性能,但生产率更高,则使用该语言是有意义的。

There was this question on stackoverflow some time ago, it may help you to figure out some reasons why F# could be faster. 前段时间在stackoverflow上存在这个问题,它可以帮助您找出F#可以更快的一些原因。 The performance gain depends very much on the application and the coding features you are using though. 性能的提高在很大程度上取决于应用程序和所使用的编码功能。

You might be interested in the article Performance-Related Features in F# and C# . 您可能对F#和C#中的性能相关功能一文感兴趣。 As for "why use F#" I can't speak for you, but for myself I use F# because I like it better. 至于“为什么要使用F#”,我不能为您说话,但是对于我自己,我使用F#是因为我更喜欢它。

There could be any number of reasons for code written in one language to be faster than code written in the other, even if they use the same runtime library. 出于多种原因,即使使用相同的运行时库,用一种语言编写的代码要比用另一种语言编写的代码快。 However, the .NET runtime library isn't the only runtime library involved. 但是,.NET运行时库不是唯一涉及的运行时库。 As I understand it, there is a rather large F# runtime library that does things that are F#-specific. 据我了解,有一个相当大的F#运行时库,可以执行特定于F#的事情。 The F# compiler knows about that library. F#编译器知道该库。 The C# compiler doesn't. C#编译器没有。 So the F# compiler can make calls to highly optimized runtime library code that the C# compiler doesn't have access to. 因此,F#编译器可以调用C#编译器无法访问的高度优化的运行时库代码。

If they're both using the same set of runtime libraries, then I would expect the programs to be close to the same, but not exact. 如果它们都使用同一组运行时库,那么我希望程序接近相同,但不完全相同。 The individual compilers could generate more or less optimum code for particular constructs. 各个编译器可以为特定构造生成或多或少的最佳代码。

There is no reason to switch but I suggest components built to meet the best standards. 没有理由进行切换,但是我建议为满足最佳标准而构建的组件。 F# enforces alot of standards, makes real time testing during development simple, and cures issues of mutability, circular dependency, as well as most type checking / null references. F#强制执行许多标准,简化开发过程中的实时测试,并解决可变性,循环依赖以及大多数类型检查/空引用的问题。 C# on the other hand has the majority of developers and tools and can be developed with a functional pattern in mind but not always correct. 另一方面,C#具有大多数开发人员和工具,可以在考虑功能模式的情况下进行开发,但并不总是正确的。 I'm a C# fan and almost always choose C# but I see many times when F# is what I need most to build cleaner robust code. 我是C#迷,几乎总是选择C#,但是我看到很多次,当我最需要F#来构建更清晰,更强大的代码时。 I guess, I personally prefer to blend the two languages when I can but as mentioned it's more important to work with what your dev team can also maintain and review. 我想,我个人比较喜欢将两种语言融合在一起,但是如上所述,与您的开发团队可以维护和审查的内容一起工作更重要。 In many situations, the dev team will be against it but in some they will be for it because they also want not only to make the code base as strong as possible but learn new things. 在许多情况下,开发团队会反对它,但在某些情况下他们会这么做,因为他们不仅希望使代码库尽可能强大,而且还希望学习新知识。 Just don't waste too much time deciding the correct language, if you're certain F# is best, present it and see if you have a consensus to complete it that way. 只是不要浪费太多时间来决定正确的语言,如果您确定F#是最好的,请展示它并查看是否有达成这种方式的共识。 Keep your project separate, even if they are in the same solution, and use F# when it works best and C# when it works best. 即使您的项目在相同的解决方案中,也要保持项目分开,并在最佳效果下使用F#,在最佳效果下使用C#。 F# isn't that hard to learn, but to a purely imperative developer it might look ugly and confusing at first. F#并不是那么难学,但是对于一个纯粹的命令式开发人员来说,一开始看起来可能很难看且令人困惑。 Just explain it some and I think it gets the ball rolling. 只是解释一下,我认为它可以使事情顺利进行。 Times to consider F# would be in algorithms where you not only want performance but immediate testing of the results. 有时需要在算法中考虑F#,您不仅需要性能,还要立即测试结果。 This speeds up dev time and runtime, win win. 这加快了开发时间和运行时间,双赢。 Others might be models if the models consist of many immutable values. 如果模型由许多不可变值组成,则其他模型也可能是模型。 F# works in all areas but for me the UI side is done easier in C# as well as things like ViewModels where logic is slightly different but that's an opinion. F#可以在所有领域使用,但对我来说,在C#中以及在ViewModels之类的界面上,逻辑方面稍有不同,但UI方面却比较容易,但这是一种看法。 You'll be surprised at the gains you get from a sweet combination of the two. 您会惊讶地发现两者完美结合所带来的收益。 Besides, functional programming is getting easier with linq, lambda, and newer C# syntax where C# basically looks functional so your pitch to use F# may not be as tough as it would years ago. 此外,使用linq,lambda和更新的C#语法使函数式编程变得更加容易,因为C#基本上看起来是函数式的,因此您使用F#的初衷可能并不像几年前那么难。
I didn't cover anything to the max but you get the idea...F# is powerful, use it when it works best and the same for C#. 我没有介绍任何东西,但您有个主意... F#功能强大,请在效果最佳的情况下使用它,并且与C#相同。 I started with VB.NET but wouldn't recommend it anymore simply because newer tech like .NET Core isn't supporting it. 我从VB.NET开始,但不再推荐它,只是因为.NET Core等较新的技术不支持它。 However, there is also a benefit to use it at times... Although be it far less than a benefit of F#. 但是,有时使用它也有好处……尽管远不及F#的好处。 In that regard I just stick to the two primaries C#, F#. 在这方面,我只是坚持使用两个主要的C#,F#。

The answer isn't the performance in speed or resources BC they are nearly the same. 答案不是速度或资源BC的性能几乎相同。 Some debated enhancements and cons... The reason for F# is development and deployment time. 一些争论的增强和缺点。F#的原因是开发和部署时间。 F# is more streamlined and eager to build with safer code. F#更加简化,并渴望使用更安全的代码进行构建。 Less error prone and more exact without worry about simple things like loop counters and type checking, cross threading etc. Therefore it is not only easier and faster to write but far less buggy. 更少的错误倾向和更准确的结果,而不必担心诸如循环计数器和类型检查,交叉线程等简单的事情。因此,它不仅更容易,更快地编写代码,而且错误更少。 You get more of what you really wanted without worrying about the petty things. 您可以获得更多您真正想要的东西,而不必担心琐碎的事情。 This means less testing and more live deployment, more trusted code and more precise functions that don't carry unneeded overhead. 这意味着更少的测试和更多的实时部署,更多值得信赖的代码以及更精确的功能,这些都不会带来不必要的开销。 Therefore the bottom line result is faster written code, optimized performance by nature, and equally as much trust with less error. 因此,最重要的结果是更快的编写代码,本质上优化的性能以及同等的信任和更少的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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