C#/F# 性能比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/144227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# / F# Performance comparison
提问by Vijesh VP
Is there any C#/F# performance comparison available on web to show proper usage of new F# language?
网络上是否有任何 C#/F# 性能比较来显示新 F# 语言的正确使用?
回答by Brian R. Bondy
See these questions that I asked recently:
看看我最近问的这些问题:
回答by Benjol
Here are a few links on (or related to) this topic:
以下是有关(或相关)此主题的一些链接:
- http://cs.hubfs.net/forums/thread/3207.aspx
- http://strangelights.com/blog/archive/2007/06/17/1588.aspx
- http://khigia.wordpress.com/2008/03/30/ocaml-vs-f-for-big-integer-surprising-performance-test/
- http://cs.hubfs.net/blogs/f_team/archive/2006/08/15/506.aspx
- http://blogs.msdn.com/jomo_fisher/
- http://cs.hubfs.net/forums/thread/3207.aspx
- http://strangelights.com/blog/archive/2007/06/17/1588.aspx
- http://khgia.wordpress.com/2008/03/30/ocaml-vs-f-for-big-integer-surprising-performance-test/
- http://cs.hubfs.net/blogs/f_team/archive/2006/08/15/506.aspx
- http://blogs.msdn.com/jomo_fisher/
What I seem to remember from another post on Robert Pickering's blog (or was it Scott Hanselman?) that in the end, because both are sitting on the same framework, you canget the same performance from both, but you sometimes have to 'twist' the natural expression of the language to do so. In the example I recall, he had to twist F# to get comparable performance with C#...
我似乎从 Robert Pickering 的博客(或者是 Scott Hanselman?)的另一篇文章中记得,最后,因为两者都坐在同一个框架上,所以你可以从两者中获得相同的性能,但有时你必须“扭曲” ' 语言的自然表达就是这样做的。在我记得的例子中,他不得不改变 F# 以获得与 C# 相当的性能......
回答by Stefan Savev
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# 代码(例如功能性/不可变)比自然(命令性/可变面向对象)C# 代码慢。但是,这种 F# 比通常的 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.
另一方面,在大多数情况下,您可以实现与 C# 代码性能相同的 F# 代码性能。这通常需要以命令式或可变的面向对象风格进行编码,分析并消除瓶颈。您可以使用在 C# 中使用的相同工具:例如 .Net 反射器和分析器。
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:
尽管如此,了解 F# 中一些会降低性能的高效率构造是值得的。根据我的经验,我见过以下案例:
references (vs. class instance variables), only in code executed billions of times
F# comparison (<=) vs. System.Collections.Generic.Comparer, for example in binary search or sort
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# 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.
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.
Try using inline to improve performance, especially for generic code.
引用(与类实例变量相比),仅在执行数十亿次的代码中
F# 比较 (<=) 与 System.Collections.Generic.Comparer,例如在二进制搜索或排序中
尾调用——仅在编译器或 .Net 运行时无法优化的某些情况下。如评论中所述,取决于 .Net 运行时。
F# 序列比 LINQ 慢两倍。这是由于引用和使用 F# 库中的函数来实现 seq<_> 的翻译。这很容易修复,因为您可以将 Seq 模块替换为使用 Linq、PLinq 或 DryadLinq 的具有相同签名的模块。
元组,F#元组是在堆上排序的类。在某些情况下,例如 int*int 元组可能需要使用结构体。
分配,值得记住的是,闭包是一个类,使用 new 操作符创建,它记住访问的变量。可能值得“解除”闭包,或者用显式将访问的变量作为参数的函数替换它。
尝试使用内联来提高性能,尤其是对于通用代码。
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# 编码,然后只优化重要的部分。在某些情况下,用 C# 编写慢函数可能比尝试调整 F# 更容易。但是,从程序员效率的角度来看,在 F# 中启动/原型化然后分析、反汇编和优化是有意义的。
Bottom line is, your F# code might end-up slower than C# because of program design decisions, but ultimately efficiency can be obtained.
最重要的是,由于程序设计决策,您的 F# 代码最终可能比 C# 慢,但最终可以获得效率。