C#/ F#的性能比较 [英] C# / F# Performance comparison

查看:607
本文介绍了C#/ F#的性能比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有任何C#/ F#的性能比较可通过网站,以显示新的F#语言的正确用法?

Is there any C#/F# performance comparison available on web to show proper usage of new F# language?

推荐答案

自然F#code(例如功能性/不可改变的)比天然(势在必行/可变面向对象)C#code慢。然而,这种F#的比通常的C#code短得多。 很显然,有一种折衷。

Natural F# code (e.g. functional/immutable) is slower than natural (imperative/mutable object-oriented) C# code. However, this kind of F# is much shorter than usual C# code. Obviously, there is a trade-off.

在另一方面,你可以在大多数情况下,实现了F#code等于C#code的性能表现。这通常需要编码必要的或可变的面向对象的风格,轮廓和消除瓶颈。您可以使用,否则你将在C#中使用相同的工具:例如:净反射器和分析器。

On the other hand, you can, in most cases, achieve performance of F# code equal to performance of C# code. This will usually require coding in imperative or mutable object-oriented style, profile and remove bottlenecks. You use that same tools that you would otherwise use in C#: e.g. .Net reflector and a profiler.

这话说回来,就得注意降低性能在F#中一些高生产率的构造。在我的经验,我已经看到了以下情况:

That having said, it pays to be aware of some high-productivity constructs in F# that decrease performance. In my experience I have seen the following cases:

  • 引用(与类的实例变量),只是在code执行数十亿次

  • references (vs. class instance variables), only in code executed billions of times

F#的比较(小于=)与System.Collections.Generic.Comparer,例如在二进制搜索或排序

F# comparison (<=) vs. System.Collections.Generic.Comparer, for example in binary search or sort

尾调用 - 只有在不能由编译器或.NET运行时进行优化某些情况下。正如在评论中指出,依赖于.NET运行时。

tail calls -- only in certain cases that cannot be optimized by the compiler or .Net runtime. As noted in the comments, depends on the .Net runtime.

F#序列比LINQ的两倍慢。这是由于参考文献和在F#库功能的使用来实现SEQ&所述的翻译; _>。这是很容易可以解决的,因为你可能会更换序列模块,一个与使用LINQ,PLINQ或DryadLinq相同的签名。

F# sequences are twice slower than LINQ. This is due to references and the use of functions in F# library to implement translation of seq<_>. This is easily fixable, as you might replace the Seq module, by one with same signatures that uses Linq, PLinq or DryadLinq.

元组,F#的元组是一类排序堆。在一些情况下,例如一个为int * INT元组可能支付使用结构。

Tuples, F# tuple is a class sorted on the heap. In some case, e.g. a int*int tuple it might pay to use a struct.

分配,这是值得记住的是一个封闭的一类,与新的运营商,它可以记住了访问的变量创建。它可能是值得到升降机封闭出来,或具有功能,明确需要被访问的变量作为参数替换它。

Allocations, it's worth remembering that a closure is a class, created with the new operator, which remembers the accessed variables. It might be worth to "lift" the closure out, or replaced it with a function that explicitly takes the accessed variables as arguments.

请尝试使用内联以提高性能,特别是对于通用的code。

Try using inline to improve performance, especially for generic code.

我的经验是code F#中的第一和唯一的最优化的重要部分。在某些情况下,可能更容易写在C#,而该慢的函数来尝试调整的F#。不过,从程序员的效率点有意义的开始/原型F#然后轮廓,拆卸和优化。

My experience is to code in F# first and optimize only the parts that matter. In certain cases, it might be easier to write the slow functions in C# rather that to try to tweak F#. However, from programmer efficiency point of view makes sense to start/prototype in F# then profile, disassemble and optimize.

底线是,你的F#code可能最终向上比C#,因为方案设计决策慢,但可以得到最终的效率。

Bottom line is, your F# code might end-up slower than C# because of program design decisions, but ultimately efficiency can be obtained.

这篇关于C#/ F#的性能比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆