author | haftmann |
Fri, 25 Sep 2009 09:50:31 +0200 | |
changeset 32705 | 04ce6bb14d85 |
parent 31757 | c1262feb61c7 |
child 32935 | 2218b970325a |
permissions | -rw-r--r-- |
31686 | 1 |
(* Title: Pure/ML-Systems/timing.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Compiler-independent timing functions. |
|
5 |
*) |
|
6 |
||
7 |
fun start_timing () = |
|
8 |
let |
|
9 |
val real_timer = Timer.startRealTimer (); |
|
10 |
val real_time = Timer.checkRealTimer real_timer; |
|
11 |
val cpu_timer = Timer.startCPUTimer (); |
|
12 |
val cpu_times = Timer.checkCPUTimes cpu_timer; |
|
13 |
in (real_timer, real_time, cpu_timer, cpu_times) end; |
|
14 |
||
15 |
fun end_timing (real_timer, real_time, cpu_timer, cpu_times) = |
|
16 |
let |
|
17 |
val real_time2 = Timer.checkRealTimer real_timer; |
|
18 |
val {nongc = {sys, usr}, gc = {sys = gc_sys, usr = gc_usr}} = cpu_times; |
|
19 |
val {nongc = {sys = sys2, usr = usr2}, gc = {sys = gc_sys2, usr = gc_usr2}} = |
|
20 |
Timer.checkCPUTimes cpu_timer; |
|
21 |
||
31757
c1262feb61c7
end_timing: checked divisions with sane defaults;
wenzelm
parents:
31693
diff
changeset
|
22 |
fun checked x y = Real.checkFloat y handle Overflow => x | Div => x; |
c1262feb61c7
end_timing: checked divisions with sane defaults;
wenzelm
parents:
31693
diff
changeset
|
23 |
|
31686 | 24 |
open Time; |
25 |
val elapsed = real_time2 - real_time; |
|
26 |
val gc = gc_usr2 - gc_usr + gc_sys2 - gc_sys; |
|
27 |
val cpu = usr2 - usr + sys2 - sys + gc; |
|
31757
c1262feb61c7
end_timing: checked divisions with sane defaults;
wenzelm
parents:
31693
diff
changeset
|
28 |
val gc_percent = |
c1262feb61c7
end_timing: checked divisions with sane defaults;
wenzelm
parents:
31693
diff
changeset
|
29 |
Real.fmt (StringCvt.FIX (SOME 1)) (100.0 * checked 0.0 (toReal gc / toReal cpu)); |
c1262feb61c7
end_timing: checked divisions with sane defaults;
wenzelm
parents:
31693
diff
changeset
|
30 |
val factor = |
c1262feb61c7
end_timing: checked divisions with sane defaults;
wenzelm
parents:
31693
diff
changeset
|
31 |
Real.fmt (StringCvt.FIX (SOME 2)) (checked 1.0 (toReal cpu / toReal elapsed)); |
31686 | 32 |
val message = |
33 |
(toString elapsed ^ "s elapsed time, " ^ toString cpu ^ "s cpu time, " ^ |
|
31693
8d1861721887
end_timing: actually display GC percentage, not factor;
wenzelm
parents:
31686
diff
changeset
|
34 |
gc_percent ^ "% GC time, factor " ^ factor) handle Time => ""; |
31686 | 35 |
in {message = message, elapsed = elapsed, cpu = cpu, gc = gc} end; |
36 |