Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Last active April 21, 2018 01:18
Show Gist options
  • Save jtpaasch/7e78e014b8b8b833f8407ded179f08da to your computer and use it in GitHub Desktop.
Save jtpaasch/7e78e014b8b8b833f8407ded179f08da to your computer and use it in GitHub Desktop.
Times function running time (OCaml)
(* Compile with unix.cma(x). *)
type timetrial = {
time : float;
result : string;
}
let time_one f x =
let start = Unix.gettimeofday () in
let result = f x in
let stop = Unix.gettimeofday () in
let delta = stop -. start in
{ time = delta; result = result }
let rec collect acc repeats results f x =
let result = f x in
match (acc < repeats) with
| false -> results
| true -> collect (acc + 1) repeats (results @ [result]) f x
let repeat n f x =
collect 0 n [] f x
let main =
let trials = repeat 5 Unix.sleepf 0.25
let () =
main;
print_endline "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment