OCaml:测量顶层的执行时间

UnS*_*Sat 5 ocaml ocaml-toplevel

我们应该如何测量OCaml顶层函数的执行时间?

Jef*_*eld 11

正如@ user3075773所说,你可以使用Sys.time.但请注意,它会返回处理器时间(CPU时间).我经常想知道挂钟时间(经过时间).你可以从这个Unix.gettimeofday:

let time f x =
    let t = Unix.gettimeofday () in
    let fx = f x in
    Printf.printf "execution elapsed time: %f sec\n"
        (Unix.gettimeofday () -. t);
    fx
Run Code Online (Sandbox Code Playgroud)


use*_*773 7

let time f x =
    let t = Sys.time() in
    let fx = f x in
    Printf.printf "execution time: %fs\n" (Sys.time() -. t);
    fx
Run Code Online (Sandbox Code Playgroud)

使用任何功能,它将打印该功能在顶层运行多长时间.