Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml: measure execution time in toplevel

How should we measure the execution time of a function in the OCaml toplevel?

like image 901
UnSat Avatar asked Dec 16 '14 17:12

UnSat


People also ask

Does OCaml have a REPL?

This chapter describes the toplevel system for OCaml, that permits interactive use of the OCaml system through a read-eval-print loop (REPL). In this mode, the system repeatedly reads OCaml phrases from the input, then typechecks, compile and evaluate them, then prints the inferred type and result value, if any.

How do I get out of toplevel OCaml?

In a terminal window, type utop to start the interactive OCaml session, commonly called the toplevel. Press Control-D to exit the toplevel.

What is :: In OCaml?

:: means 2 camel humps, ' means 1 hump! – Nick Craver. Feb 27, 2010 at 12:09. Ok a decent comment: merd.sourceforge.net/pixel/language-study/… I don't use oCaml, but there's a full syntax list you my find useful, even if there's no real context to it.

What is OCaml top level?

In short, the toplevel is OCaml's Read-Eval-Print-Loop (repl) allowing interative use of the OCaml system. You can consider the toplevel an alternative to compiling OCaml into an executable.


1 Answers

As @user3075773 says, you can use Sys.time. However note that it returns processor time (CPU time). More often I want to know the wall clock time (elapsed time). You can get this from 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
like image 188
Jeffrey Scofield Avatar answered Sep 30 '22 17:09

Jeffrey Scofield