let max_profilers = 20;; let profiler_no = ref 0;; let profiler_label2int = Hashtbl.create 3;; let name = ref "";; let banner _ pname = name := pname; "(Array.make "^string_of_int max_profilers^" (0,0.)), (Array.make "^string_of_int max_profilers^" (0.))" ;; let profile_start _ label = if !profiler_no > max_profilers then raise (Invalid_argument "Too many profilers."); if not (Hashtbl.mem profiler_label2int label) then begin Hashtbl.add profiler_label2int label !profiler_no; incr profiler_no; end; let id = Hashtbl.find profiler_label2int label in " ((snd "^ !name^").("^string_of_int id^") <- Unix.gettimeofday()) " ;; let profile_stop _ label = if not (Hashtbl.mem profiler_label2int label) then raise (Invalid_argument "Profiler 'stop' before 'begin'."); let id = Hashtbl.find profiler_label2int label in " ( let interval = Unix.gettimeofday () -. (snd "^ !name^").("^string_of_int id^") in let oldcount,oldval = (fst "^ !name^").("^string_of_int id^") in (fst "^ !name^").("^string_of_int id^") <- (oldcount+1,interval +. oldval) ) " ;; let profile_show _ _ = (Hashtbl.fold (fun k v acc -> acc ^ "let t = (fst "^ !name^").("^string_of_int v^") in "^ "let acc = acc ^ Printf.sprintf \"%20s: %5d %8.4f\" \""^k^"\" (fst t) (snd t) in") profiler_label2int "let acc = \"\" in ") ^ " acc " ;; Quotation.add "profiler" (Quotation.ExStr banner);; Quotation.add "start" (Quotation.ExStr profile_start);; Quotation.add "stop" (Quotation.ExStr profile_stop);; Quotation.add "show" (Quotation.ExStr profile_show);;