Skip to content

Instantly share code, notes, and snippets.

@jyc
Last active October 7, 2015 05:02
Show Gist options
  • Select an option

  • Save jyc/dc6d8a771bc472bfd868 to your computer and use it in GitHub Desktop.

Select an option

Save jyc/dc6d8a771bc472bfd868 to your computer and use it in GitHub Desktop.
let times = Hashtbl.create 1
let time_start' ht id =
let (sum, _, n) =
if Hashtbl.mem ht id then Hashtbl.find ht id
else (0., 0., 0) in
Hashtbl.replace ht id (sum, Sys.time (), n)
let time_start = time_start' times
let time_end' ht id =
if not (Hashtbl.mem ht id) then failwith "invalid state!"
else
let (sum, last, n) = Hashtbl.find ht id in
let (sum', last', n') = (sum +. (Sys.time () -. last), 0., n + 1) in
Hashtbl.replace ht id (sum', last', n')
let time_end = time_end' times
let print_times () =
print_endline "=== TIMES ===" ;
let print_time id (sum, last, n) =
let avg = sum /. (float_of_int n) in
Printf.printf "[%s] average time: %f, number of times: %d\n" id avg n
in
Hashtbl.iter print_time times
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment