OCaml: measuring runtime at the top

How to measure function runtime in OCaml toplevel?

+5
source share
3 answers

As @ user3075773 reports, you can use Sys.time . However, note that it returns processor time (processor time). Most often, I want to find out the wall clock time (elapsed time). You can get this from Unix.gettimeofday :

 let time fx = let t = Unix.gettimeofday () in let fx = fx in Printf.printf "execution elapsed time: %f sec\n" (Unix.gettimeofday () -. t); fx 
+11
source
 let time fx = let t = Sys.time() in let fx = fx in Printf.printf "execution time: %fs\n" (Sys.time() -. t); fx 

It works with any function, it will print how long the function has been running to run in the upper level.

+7
source

Using gettimeofday , like the accepted answer, is not a good idea, since it is sensitive to the settings of the calendar time operating system (for example, via ntp ).

If you just need CPU time, then using Sys.time great. If you want a wall clock, you should use a monotonous time source. One of them is available for OCaml in the mtime package.

+1
source

Source: https://habr.com/ru/post/1209257/


All Articles