How to figure out the amount of time my code has taken in ocaml? are there any functions to measure that?
If you want to measure execution time of individual functions, this utility function is helpful in many cases:
let time f x =
let t = Sys.time() in
let fx = f x in
Printf.printf "Execution time: %fs\n" (Sys.time() -. t);
fx
where f
is any function which takes x
as the argument and returns something.
In my own coding, I use Unix.gettimeofday ()
, which returns a float value with a resolution of much less than one second. It's described in the documentation for the OCaml Unix module. Despite the name of the module, this function also works in Windows (and probably in almost all environments you might encounter).
If I rewrite pad's answer I get the following code:
let time f x =
let start = Unix.gettimeofday ()
in let res = f x
in let stop = Unix.gettimeofday ()
in let () = Printf.printf "Execution time: %fs\n%!" (stop -. start)
in
res
You can use Sys.time(). It returns the processor time, in seconds, used by the program since the beginning of execution.
Source : http://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With