Last active
April 21, 2018 01:18
-
-
Save jtpaasch/7e78e014b8b8b833f8407ded179f08da to your computer and use it in GitHub Desktop.
Times function running time (OCaml)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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