src/Pure/General/timing.ML
author wenzelm
Fri Nov 25 18:37:14 2011 +0100 (2011-11-25 ago)
changeset 45633 2cb7e34f6096
parent 44440 aa2abd81f460
child 50777 20126dd9772c
permissions -rw-r--r--
retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
wenzelm@40393
     1
(*  Title:      Pure/General/timing.ML
wenzelm@31686
     2
    Author:     Makarius
wenzelm@31686
     3
wenzelm@40336
     4
Basic support for time measurement.
wenzelm@31686
     5
*)
wenzelm@31686
     6
wenzelm@42012
     7
signature BASIC_TIMING =
wenzelm@42012
     8
sig
wenzelm@42012
     9
  val cond_timeit: bool -> string -> (unit -> 'a) -> 'a
wenzelm@42012
    10
  val timeit: (unit -> 'a) -> 'a
wenzelm@42012
    11
  val timeap: ('a -> 'b) -> 'a -> 'b
wenzelm@42012
    12
  val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b
wenzelm@42012
    13
end
wenzelm@40300
    14
wenzelm@42012
    15
signature TIMING =
wenzelm@42012
    16
sig
wenzelm@42012
    17
  include BASIC_TIMING
wenzelm@42012
    18
  type timing = {elapsed: Time.time, cpu: Time.time, gc: Time.time}
wenzelm@42012
    19
  type start
wenzelm@42012
    20
  val start: unit -> start
wenzelm@42012
    21
  val result: start -> timing
wenzelm@42013
    22
  val timing: ('a -> 'b) -> 'a -> timing * 'b
wenzelm@44440
    23
  val is_relevant: timing -> bool
wenzelm@42012
    24
  val message: timing -> string
wenzelm@42012
    25
end
wenzelm@42012
    26
wenzelm@42012
    27
structure Timing: TIMING =
wenzelm@42012
    28
struct
wenzelm@42012
    29
wenzelm@42012
    30
(* timer control *)
wenzelm@42012
    31
wenzelm@42012
    32
type timing = {elapsed: Time.time, cpu: Time.time, gc: Time.time};
wenzelm@42012
    33
wenzelm@42012
    34
abstype start = Start of
wenzelm@42012
    35
  Timer.real_timer * Time.time * Timer.cpu_timer *
wenzelm@42012
    36
    {gc: {sys: Time.time, usr: Time.time}, nongc: {sys: Time.time, usr: Time.time}}
wenzelm@42012
    37
with
wenzelm@42012
    38
wenzelm@42012
    39
fun start () =
wenzelm@31686
    40
  let
wenzelm@31686
    41
    val real_timer = Timer.startRealTimer ();
wenzelm@31686
    42
    val real_time = Timer.checkRealTimer real_timer;
wenzelm@31686
    43
    val cpu_timer = Timer.startCPUTimer ();
wenzelm@31686
    44
    val cpu_times = Timer.checkCPUTimes cpu_timer;
wenzelm@42012
    45
  in Start (real_timer, real_time, cpu_timer, cpu_times) end;
wenzelm@31686
    46
wenzelm@42012
    47
fun result (Start (real_timer, real_time, cpu_timer, cpu_times)) =
wenzelm@31686
    48
  let
wenzelm@31686
    49
    val real_time2 = Timer.checkRealTimer real_timer;
wenzelm@31686
    50
    val {nongc = {sys, usr}, gc = {sys = gc_sys, usr = gc_usr}} = cpu_times;
wenzelm@31686
    51
    val {nongc = {sys = sys2, usr = usr2}, gc = {sys = gc_sys2, usr = gc_usr2}} =
wenzelm@31686
    52
      Timer.checkCPUTimes cpu_timer;
wenzelm@31686
    53
wenzelm@31686
    54
    open Time;
wenzelm@31686
    55
    val elapsed = real_time2 - real_time;
wenzelm@31686
    56
    val gc = gc_usr2 - gc_usr + gc_sys2 - gc_sys;
wenzelm@31686
    57
    val cpu = usr2 - usr + sys2 - sys + gc;
wenzelm@42012
    58
  in {elapsed = elapsed, cpu = cpu, gc = gc} end;
wenzelm@32935
    59
wenzelm@42012
    60
end;
wenzelm@42012
    61
wenzelm@42013
    62
fun timing f x =
wenzelm@42013
    63
  let
wenzelm@42013
    64
    val start = start ();
wenzelm@42013
    65
    val y = f x;
wenzelm@42013
    66
  in (result start, y) end;
wenzelm@42013
    67
wenzelm@42012
    68
wenzelm@42012
    69
(* timing messages *)
wenzelm@42012
    70
wenzelm@42819
    71
val min_time = Time.fromMilliseconds 1;
wenzelm@42819
    72
wenzelm@42819
    73
fun is_relevant {elapsed, cpu, gc} =
wenzelm@42819
    74
  Time.>= (elapsed, min_time) orelse
wenzelm@42819
    75
  Time.>= (cpu, min_time) orelse
wenzelm@42819
    76
  Time.>= (gc, min_time);
wenzelm@42819
    77
wenzelm@44440
    78
fun message {elapsed, cpu, gc} =
wenzelm@44440
    79
  Time.toString elapsed ^ "s elapsed time, " ^
wenzelm@44440
    80
  Time.toString cpu ^ "s cpu time, " ^
wenzelm@44440
    81
  Time.toString gc ^ "s GC time" handle Time.Time => "";
wenzelm@44440
    82
wenzelm@42012
    83
fun cond_timeit enabled msg e =
wenzelm@42012
    84
  if enabled then
wenzelm@42012
    85
    let
wenzelm@42042
    86
      val (timing, result) = timing (Exn.interruptible_capture e) ();
wenzelm@42819
    87
      val _ =
wenzelm@42819
    88
        if is_relevant timing then
wenzelm@42819
    89
          let val end_msg = message timing
wenzelm@42819
    90
          in warning (if msg = "" then end_msg else msg ^ "\n" ^ end_msg) end
wenzelm@42819
    91
        else ();
wenzelm@42013
    92
    in Exn.release result end
wenzelm@42012
    93
  else e ();
wenzelm@42012
    94
wenzelm@42012
    95
fun timeit e = cond_timeit true "" e;
wenzelm@42012
    96
fun timeap f x = timeit (fn () => f x);
wenzelm@42012
    97
fun timeap_msg msg f x = cond_timeit true msg (fn () => f x);
wenzelm@42012
    98
wenzelm@42012
    99
end;
wenzelm@42012
   100
wenzelm@42012
   101
structure Basic_Timing: BASIC_TIMING = Timing;
wenzelm@42012
   102
open Basic_Timing;
wenzelm@42012
   103