F# and C# 's CLR is same then why is F# faster than C#

前端 未结 7 736
失恋的感觉
失恋的感觉 2021-01-18 02:42

I am confused and would appreciate if you enlighten me. F# uses same CLR as C# and underlying code is identical then how can one suggest that a function runs faster when wr

7条回答
  •  说谎
    说谎 (楼主)
    2021-01-18 02:58

    1) If you write C# code and F# code which gets compiled to exactly the same IL (the CLR's intermediate language), then the code will exhibit the same performance characteristics. However, implementing the same algorithm in C# and F# does not guarantee that exactly the same IL will be generated, so real world performance may vary. I believe that in practice sometimes F# code will be faster but other times C# will be faster.

    2) There are many reasons to choose an implementation language besides performance. For example, some people find F# to be more succinct, readable, and easier to reason about than C# for certain problem domains, which would be one reason to prefer it. For the vast majority of programs, a 5% or 10% difference in performance is going to be imperceptible to users, so if a language offers roughly comparable performance but superior productivity then it would make sense to use that language.

提交回复
热议问题