微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在为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.jb51.cc/csharp/99843.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐