Created
December 3, 2016 13:16
-
-
Save aantron/f9c98ea4a682c19c71a247ae5dd1e608 to your computer and use it in GitHub Desktop.
Profiling Markup.ml
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
(* ocamlfind opt -linkpkg -package unix -package markup measure.ml *) | |
let measure f = | |
let open Unix in | |
let start_times = times () in | |
let start_wall = gettimeofday () in | |
f (); | |
let end_times = times () in | |
let end_wall = gettimeofday () in | |
let total_times = | |
end_times.tms_utime -. start_times.tms_utime +. | |
end_times.tms_stime -. start_times.tms_stime | |
in | |
let total_wall = end_wall -. start_wall in | |
total_times, total_wall | |
let measure_repeatedly n f = | |
let rec loop times wall = function | |
| 0 -> times, wall | |
| n -> | |
let times', wall' = measure f in | |
loop (times +. times') (wall +. wall') (n - 1) | |
in | |
let times, wall = loop 0. 0. n in | |
let n = float_of_int n in | |
times /. n, wall /. n | |
let () = | |
let html, close = Markup.file Sys.argv.(1) in | |
let html = Markup.to_string html in | |
close (); | |
let cpu_time, wall_time = | |
measure_repeatedly 100 (fun () -> | |
Markup.(string html |> parse_html |> signals |> drain)) | |
in | |
(* Convert to milliseconds. *) | |
let cpu_time = cpu_time *. 1000. in | |
let wall_time = wall_time *. 1000. in | |
Printf.printf "cpu: %.1f ms\nwall: %.1f ms\n" cpu_time wall_time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment