Measure time of execution in F#

后端 未结 4 774
走了就别回头了
走了就别回头了 2021-01-01 10:30

Please post code for displaying time in F#. I noticed that you can measure it from F# interactive with #time directive, but I don\'t know how to execute program from FSI

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 11:18

    Check out the timer function in the F Sharp Programming wikibook. It is defined like this:

    let duration f = 
        let timer = new System.Diagnostics.Stopwatch()
        timer.Start()
        let returnValue = f()
        printfn "Elapsed Time: %i" timer.ElapsedMilliseconds
        returnValue    
    

    Whilst being used like this:

    let sample() = System.Threading.Thread.Sleep(2)
    
    duration ( fun() -> sample() )
    // or
    duration sample
    

提交回复
热议问题