src/Pure/ML-Systems/timing.ML
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 32935 2218b970325a
child 40300 d4487353b3a0
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
wenzelm@31686
     1
(*  Title:      Pure/ML-Systems/timing.ML
wenzelm@31686
     2
    Author:     Makarius
wenzelm@31686
     3
wenzelm@31686
     4
Compiler-independent timing functions.
wenzelm@31686
     5
*)
wenzelm@31686
     6
wenzelm@31686
     7
fun start_timing () =
wenzelm@31686
     8
  let
wenzelm@31686
     9
    val real_timer = Timer.startRealTimer ();
wenzelm@31686
    10
    val real_time = Timer.checkRealTimer real_timer;
wenzelm@31686
    11
    val cpu_timer = Timer.startCPUTimer ();
wenzelm@31686
    12
    val cpu_times = Timer.checkCPUTimes cpu_timer;
wenzelm@31686
    13
  in (real_timer, real_time, cpu_timer, cpu_times) end;
wenzelm@31686
    14
wenzelm@31686
    15
fun end_timing (real_timer, real_time, cpu_timer, cpu_times) =
wenzelm@31686
    16
  let
wenzelm@31686
    17
    val real_time2 = Timer.checkRealTimer real_timer;
wenzelm@31686
    18
    val {nongc = {sys, usr}, gc = {sys = gc_sys, usr = gc_usr}} = cpu_times;
wenzelm@31686
    19
    val {nongc = {sys = sys2, usr = usr2}, gc = {sys = gc_sys2, usr = gc_usr2}} =
wenzelm@31686
    20
      Timer.checkCPUTimes cpu_timer;
wenzelm@31686
    21
wenzelm@31686
    22
    open Time;
wenzelm@31686
    23
    val elapsed = real_time2 - real_time;
wenzelm@31686
    24
    val gc = gc_usr2 - gc_usr + gc_sys2 - gc_sys;
wenzelm@31686
    25
    val cpu = usr2 - usr + sys2 - sys + gc;
wenzelm@32935
    26
wenzelm@31686
    27
    val message =
wenzelm@31686
    28
      (toString elapsed ^ "s elapsed time, " ^ toString cpu ^ "s cpu time, " ^
wenzelm@32935
    29
        toString gc ^ "s GC time") handle Time.Time => "";
wenzelm@31686
    30
  in {message = message, elapsed = elapsed, cpu = cpu, gc = gc} end;
wenzelm@31686
    31