c# - 在为 C# 程序编写数学函数时,F# 的性能是否良好?

标签 c# performance f#

有谁知道与 C# 相比,F# 在性能方面的衡量结果如何。我有一个 C# 光线跟踪器,其中包含大量矢量操作、光线碰撞算法等,我认为它们可能更容易在 F# 中表达。我不是在问 F# 在表达数学问题方面有多好,这已经得到解答 here ,而是我是否应该期望更好或更差的性能?由于光线追踪非常注重性能,即使是性能不佳的小案例也可能在错误的地方成为问题。

编辑:

似乎已经有很多关于这个主题的问题我找不到了(如果你真的用“F#”这个词搜索任何东西,没有结果)。一个好点here是以下答案:

F# provides some performance-related features that can make a difference.

Firstly, the implementation of delegates on .NET is currently quite inefficient and, consequently, F# uses its own FastFunc type for high-performance first-class functions.

Secondly, F# uses .NET metadata to convey inline functions so that they can be exported across APIs and, of course, that can dramatically improve performance in certain circumstances.

Finally, pattern matching can be extremely laborious to express in C# because the language lacks pattern matching but it is almost impossible to maintain optimized C# code equivalent to many non-trivial pattern matches. In contrast, the F# compiler aggressively optimizes pattern matches during compilation.

Conversely, the C# compiler is better at optimizing loops using IEnumerables and is better at optimizing computations over value types (e.g. complex arithmetic).

Cheers, Jon Harrop.

最佳答案

是的,F# 会表现得更好。

以下是用不同语言实现的单线程算法的一些性能结果(神经网络的基准激活函数方法):

C#:

10^7 iterations using Sigmoid1() took 3899,1979 ms
10^7 iterations using Sigmoid2() took 411,4441 ms

纯 C:

10^7 iterations using sigmoid1: 628 ms
10^7 iterations using sigmoid2: 157 ms

F#:

10^7 iterations using sigmoid1: 588.843700 ms
10^7 iterations using sigmoid2: 156.626700 ms

More details

关于c# - 在为 C# 程序编写数学函数时,F# 的性能是否良好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/431444/

相关文章:

c# - 我的双击绑定(bind)不起作用

php - 我如何计时PHP的microtime本身?

f# - F#中的平方根函数

xml - F# Xml Type Provider 测试节点是否存在

c# - CLR 如何访问 C# 堆栈?

c# - 获取文件扩展名而不在路径中提供扩展名

java - 数据映射优化 : X database queries vs. 是 Java if 语句

c# - 使用 C# 的 Windows 服务和性能信息

arrays - 快速将文本数据读入数组

c# - 使用带有 CaSTLe 代理拦截器的简单注入(inject)器