Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure time of execution in F#

Tags:

time

measure

f#

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

like image 859
nanek Avatar asked Dec 24 '10 21:12

nanek


People also ask

What is execution time measured in?

CPU time. First, it is important to define and differentiate these two terms that are often used when measuring execution time. Wall time (also known as clock time or wall-clock time) is simply the total time elapsed during the measurement.

How do you calculate time in a function in C++?

measure execution time of a program. Using time() function in C & C++. time() : time() function returns the time since the Epoch(jan 1 1970) in seconds. Prototype / Syntax : time_t time(time_t *tloc);


2 Answers

I would just use the .NET Stopwatch class.

let stopWatch = System.Diagnostics.Stopwatch.StartNew() ... stopWatch.Stop() printfn "%f" stopWatch.Elapsed.TotalMilliseconds 
like image 66
BrokenGlass Avatar answered Sep 24 '22 10:09

BrokenGlass


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

like image 34
fxdxpz Avatar answered Sep 24 '22 10:09

fxdxpz