time.ml 1.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
(******************************************************************************)
(*                                                                            *)
(*                                   Menhir                                   *)
(*                                                                            *)
(*                       François Pottier, Inria Paris                        *)
(*              Yann Régis-Gianas, PPS, Université Paris Diderot              *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the GNU General Public License version 2, as described in the    *)
(*  file LICENSE.                                                             *)
(*                                                                            *)
(******************************************************************************)

14 15 16 17 18 19 20
open Unix
open Printf

let clock =
  ref (times())

let tick msg =
21 22 23 24 25 26 27 28 29 30
  match Settings.timings with
  | None ->
      ()
  | Some channel ->
      let times1 = !clock in
      let times2 = times() in
      fprintf channel "%s: %.02fs\n%!"
        msg
        (times2.tms_utime -. times1.tms_utime);
      clock := times()
31 32 33 34 35 36 37 38

type chrono =
    float ref

let fresh () =
  ref 0.

let chrono (chrono : float ref) (task : unit -> 'a) : 'a =
39 40 41 42 43 44 45 46 47
  match Settings.timings with
  | None ->
      task()
  | Some _channel ->
      let times1 = times() in
      let result = task() in
      let times2 = times() in
      chrono := !chrono +. times2.tms_utime -. times1.tms_utime;
      result
48 49

let display (chrono : float ref) msg =
50 51 52 53 54 55 56
  match Settings.timings with
  | None ->
      ()
  | Some channel ->
      fprintf channel "%s: %.02fs\n"
        msg
        !chrono