F# performance in scientific computing

前端 未结 10 1179
闹比i
闹比i 2020-12-22 17:12

I am curious as to how F# performance compares to C++ performance? I asked a similar question with regards to Java, and the impression I got was that Java is not suitable f

相关标签:
10条回答
  • 2020-12-22 17:52

    Here are two examples I can share:

    1. Matrix multiplication: I have a blog post comparing different matrix multiplication implementations.

    2. LBFGS

    I have a large scale logistic regression solver using LBFGS optimization, which is coded in C++. The implementation is well tuned. I modified some code to code in C++/CLI, i.e. I compiled the code into .Net. The .Net version is 3 to 5 times slower than the naive compiled one on different datasets. If you code LBFGS in F#, the performance can not be better than C++/CLI or C#, (but would be very close).

    I have another post on Why F# is the language for data mining, although not quite related to the performance issue you concern here, it is quite related to scientific computing in F#.

    0 讨论(0)
  • 2020-12-22 17:53

    In addition to what others said, there is one important point about F# and that's parallelism. The performance of ordinary F# code is determined by CLR, although you may be able to use LAPACK from F# or you may be able to make native calls using C++/CLI as part of your project.

    However, well-designed functional programs tend to be much easier to parallelize, which means that you can easily gain performance by using multi-core CPUs, which are definitely available to you if you're doing some scientific computing. Here are a couple of relevant links:

    • F# and Task-Parallel library (blog by Jurgen van Gael, who is doing machine-learning stuff)
    • Another interesting answer at SO regarding parllelism
    • An example of using Parallel LINQ from F#
    • Chapter 14 of my book discusses parallelism (source code is available)

    Regarding distributed computing, you can use any distributed computing framework that's available for the .NET platform. There is a MPI.NET project, which works well with F#, but you may be also able to use DryadLINQ, which is a MSR project.

    • Some articles: F# MPI tools for .NET, Concurrency with MPI.NET
    • DryadLINQ project hompepage
    0 讨论(0)
  • 2020-12-22 17:56

    Last I knew, most scientific computing was still done in FORTRAN. It's still faster than anything else for linear algebra problems - not Java, not C, not C++, not C#, not F#. LINPACK is nicely optimized.

    But the remark about "your mileage may vary" is true of all benchmarks. Blanket statements (except mine) are rarely true.

    0 讨论(0)
  • 2020-12-22 17:57

    As with all language/performance comparisons, your mileage depends greatly on how well you can code.

    F# is a derivative of OCaml. I was surprised to find out that OCaml is used a lot in the financial world, where number crunching performance is very important. I was further surprised to find out that OCaml is one of the faster languages, with performance on par with the fastest C and C++ compilers.

    F# is built on the CLR. In the CLR, code is expressed in a form of bytecode called the Common Intermediate Language. As such, it benefits from the optimizing capabilities of the JIT, and has performance comparable to C# (but not necessarily C++), if the code is written well.

    CIL code can be compiled to native code in a separate step prior to runtime by using the Native Image Generator (NGEN). This speeds up all later runs of the software as the CIL-to-native compilation is no longer necessary.

    One thing to consider is that functional languages like F# benefit from a more declarative style of programming. In a sense, you are over-specifying the solution in imperative languages such as C++, and this limits the compiler's ability to optimize. A more declarative programming style can theoretically give the compiler additional opportunities for algorithmic optimization.

    0 讨论(0)
提交回复
热议问题