Measure time of execution in F#
Asked Answered
H

4

39

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

Thanks

Holleyholli answered 24/12, 2010 at 21:11 Comment(1)
At the time of writing, this wasn't available, but don't use stopwatch and the like, it's extremely flawed. Instead, use Benchmarkdotnet for proper performance measuring, and a profiler.Beatup
B
58

I would just use the .NET Stopwatch class.

let stopWatch = System.Diagnostics.Stopwatch.StartNew()
...
stopWatch.Stop()
printfn "%f" stopWatch.Elapsed.TotalMilliseconds
Birr answered 24/12, 2010 at 21:47 Comment(4)
But if you're in FSI (F# Interactive REPL), you get more detailed information (CPU time, Real time, GC info). Adding PrivateEye gives even full profiling info from FSI, no extra programming needed.Beatup
@Beatup Do you happen to know if PrivateEye is still available somewhere? The website is unfortunately defunct. I even contacted one of the authors, and he told me I should still be able to find the code somewhere, but then contact broke of... :(Entropy
@sebhofer, I don't know. I only use Benchmarkdotnet nowadays, gives much info, and takes care of warmup time, averages, outliers, graphs etc etc. It's used for improving the runtime itself, it's pretty darn stable.Beatup
When I use this with AOT in 8.0-preview5, I get Unhandled Exception: System.NotSupportedException: 'Microsoft.FSharp.Core.PrintfImpl+Specializations`3[System.IO.TextWriter,Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Core.Unit].CaptureFinal1[System.Int32](Microsoft.FSharp.Core.PrintfImpl+Step[])' is missing native code. MethodInfo.MakeGenericMethod() is not compatible with AOT compilation. Inspect and fix AOT related warnings that were generated when the app was published. For more information see aka.ms/nativeaot-compatibilityGridiron
N
34

From msdn:

By itself, #time toggles whether to display performance information. When it is enabled, F# Interactive measures real time, CPU time, and garbage collection information for each section of code that is interpreted and executed.

So to test you function you have to open F# interactive console and execute your function in it (one way to do it is to select your function, right click and chose Execute in Interactive) Then make a call to your function in Interactive like that for example:

// define your function first either in interactive console or in your document:

let square x = x * x

// in interactive
#time
square 10
#time

You will see how much of real time and CPU time were spent on computation and a bit of information from garbage collector

Nashville answered 23/11, 2013 at 1:18 Comment(0)
P
22

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
Provide answered 10/1, 2011 at 10:58 Comment(2)
they are using DateTime.Now to measure time in their function, that's hardly a replacement for the Stopwatch timer.Birr
@Birr wiki was updated a while back to use stopwatch. Included here for referenceNosebleed
V
1

You could also create custom computation expression to hide actual measuring logic, e.g.:

timer {
   // your code goes here
}

See more examples here: https://fsharpforfunandprofit.com/posts/computation-expressions-bind/

Veronikaveronike answered 25/8, 2020 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.