Why is printf in F# so slow?

前端 未结 4 2027
悲&欢浪女
悲&欢浪女 2020-12-14 02:50

I\'ve just been really surprised by how slow printf from F# is. I have a number of C# programs that process large data files and write out a number of CSV files. I originall

4条回答
  •  遥遥无期
    2020-12-14 03:33

    I am not sure how much it matters, but...

    Inspecting the code for printf:

    https://github.com/fsharp/fsharp/blob/master/src/fsharp/FSharp.Core/printf.fs

    I see

    // The general technique used this file is to interpret
    // a format string and use reflection to construct a function value that matches
    // the specification of the format string.  
    

    and I think the word 'reflection' probably answers the question.

    printf is great for writing simple type-safe output, but if you want good perf in an inner loop, you might want to use a lower-level .NET API to write output. I haven't done my own benchmarking to see.

提交回复
热议问题