|
| 1 | +(* *********************************************************************) |
| 2 | +(* *) |
| 3 | +(* The Compcert verified compiler *) |
| 4 | +(* *) |
| 5 | +(* Bernhard Schommer, AbsInt Angewandte Informatik GmbH *) |
| 6 | +(* *) |
| 7 | +(* AbsInt Angewandte Informatik GmbH. All rights reserved. This file *) |
| 8 | +(* is distributed under the terms of the INRIA Non-Commercial *) |
| 9 | +(* License Agreement. *) |
| 10 | +(* *) |
| 11 | +(* *********************************************************************) |
| 12 | + |
| 13 | +open Clflags |
| 14 | + |
| 15 | +(** Timing facility *) |
| 16 | + |
| 17 | +let timers : (string * float) list ref = ref [] |
| 18 | + |
| 19 | +let add_to_timer name time = |
| 20 | + let rec add = function |
| 21 | + | [] -> [name, time] |
| 22 | + | (name1, time1 as nt1) :: rem -> |
| 23 | + if name1 = name then (name1, time1 +. time) :: rem else nt1 :: add rem |
| 24 | + in timers := add !timers |
| 25 | + |
| 26 | +let time name fn arg = |
| 27 | + if not !option_timings then |
| 28 | + fn arg |
| 29 | + else begin |
| 30 | + let start = Sys.time() in |
| 31 | + try |
| 32 | + let res = fn arg in |
| 33 | + add_to_timer name (Sys.time() -. start); |
| 34 | + res |
| 35 | + with x -> |
| 36 | + add_to_timer name (Sys.time() -. start); |
| 37 | + raise x |
| 38 | + end |
| 39 | + |
| 40 | +let time2 name fn arg1 arg2 = time name (fun () -> fn arg1 arg2) () |
| 41 | +let time3 name fn arg1 arg2 arg3 = time name (fun () -> fn arg1 arg2 arg3) () |
| 42 | + |
| 43 | +let time_coq name fn arg = |
| 44 | + if not !option_timings then |
| 45 | + fn arg |
| 46 | + else begin |
| 47 | + let start = Sys.time() in |
| 48 | + try |
| 49 | + let res = fn arg in |
| 50 | + add_to_timer (Camlcoq.camlstring_of_coqstring name) (Sys.time() -. start); |
| 51 | + res |
| 52 | + with x -> |
| 53 | + add_to_timer (Camlcoq.camlstring_of_coqstring name) (Sys.time() -. start); |
| 54 | + raise x |
| 55 | + end |
| 56 | + |
| 57 | +let print_timers () = |
| 58 | + if !option_timings then |
| 59 | + List.iter |
| 60 | + (fun (name, time) -> Printf.printf "%7.2fs %s\n" time name) |
| 61 | + !timers |
| 62 | + |
| 63 | +let _ = at_exit print_timers |
0 commit comments