在为C#程序编写数学函数时,F#能否表现良好?

前端之家收集整理的这篇文章主要介绍了在为C#程序编写数学函数时,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

原文链接:https://www.f2er.com/csharp/99843.html

猜你在找的C#相关文章