Running time in Ocaml

后端 未结 3 1537
挽巷
挽巷 2020-12-11 01:46

How to figure out the amount of time my code has taken in ocaml? are there any functions to measure that?

3条回答
  •  忘掉有多难
    2020-12-11 02:28

    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
    

提交回复
热议问题