40 val timing: bool ref |
40 val timing: bool ref |
41 val cond_timeit: bool -> (unit -> 'a) -> 'a |
41 val cond_timeit: bool -> (unit -> 'a) -> 'a |
42 val timeit: (unit -> 'a) -> 'a |
42 val timeit: (unit -> 'a) -> 'a |
43 val timeap: ('a -> 'b) -> 'a -> 'b |
43 val timeap: ('a -> 'b) -> 'a -> 'b |
44 val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b |
44 val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b |
|
45 type time_info |
|
46 val time_init: unit -> time_info ref |
|
47 val time_reset: time_info ref -> unit |
|
48 val time_check: time_info ref -> {timer: Timer.cpu_timer, |
|
49 sys: Time.time, usr: Time.time, gc: Time.time, count: int} |
|
50 val time_finish: string -> time_info ref -> unit |
|
51 val time: time_info ref -> ('a -> 'b) -> 'a -> 'b |
45 end; |
52 end; |
46 |
53 |
47 signature OUTPUT = |
54 signature OUTPUT = |
48 sig |
55 sig |
49 include BASIC_OUTPUT |
56 include BASIC_OUTPUT |
204 |
211 |
205 (*timed application function*) |
212 (*timed application function*) |
206 fun timeap f x = timeit (fn () => f x); |
213 fun timeap f x = timeit (fn () => f x); |
207 fun timeap_msg s f x = (warning s; timeap f x); |
214 fun timeap_msg s f x = (warning s; timeap f x); |
208 |
215 |
|
216 |
|
217 (* accumulated timing *) |
|
218 |
|
219 local |
|
220 |
|
221 fun add_interval time time1 time2 = |
|
222 Time.+ (time, Time.- (time2, time1) handle Time.Time => Time.zeroTime); |
|
223 |
|
224 fun secs prfx time = prfx ^ Time.toString time; |
|
225 |
|
226 in |
|
227 |
|
228 datatype time_info = TI of |
|
229 {timer: Timer.cpu_timer, |
|
230 sys: Time.time, |
|
231 usr: Time.time, |
|
232 gc: Time.time, |
|
233 count: int}; |
|
234 |
|
235 fun time_init () = ref (TI {timer = Timer.startCPUTimer (), |
|
236 sys = Time.zeroTime, usr = Time.zeroTime, gc = Time.zeroTime, count = 0}); |
|
237 |
|
238 fun time_reset r = r := ! (time_init ()); |
|
239 |
|
240 fun time_check (ref (TI r)) = r; |
|
241 |
|
242 fun time_finish name ti = |
|
243 let val {timer, sys, usr, gc, count} = time_check ti in |
|
244 warning ("Total of " ^ quote name ^ ": " ^ |
|
245 secs "User " usr ^ secs " GC " gc ^ secs " All " (Time.+ (sys, Time.+ (usr, gc))) ^ |
|
246 " secs in " ^ string_of_int count ^ " calls"); |
|
247 ti := TI {timer = timer, sys = Time.zeroTime, usr = Time.zeroTime, |
|
248 gc = Time.zeroTime, count = 0} |
|
249 end; |
|
250 |
|
251 fun time ti f x = |
|
252 let |
|
253 val {timer, sys, usr, gc, count} = time_check ti; |
|
254 val (sys1, usr1, gc1) = checkTimer timer; |
|
255 val result = capture f x; |
|
256 val (sys2, usr2, gc2) = checkTimer timer; |
|
257 in |
|
258 ti := TI {timer = timer, |
|
259 sys = add_interval sys sys1 sys2, |
|
260 usr = add_interval usr usr1 usr2, |
|
261 gc = add_interval gc gc1 gc2, |
|
262 count = count + 1}; |
|
263 release result |
|
264 end; |
|
265 |
|
266 end; |
|
267 |
209 end; |
268 end; |
210 |
269 |
211 structure BasicOutput: BASIC_OUTPUT = Output; |
270 structure BasicOutput: BASIC_OUTPUT = Output; |
212 open BasicOutput; |
271 open BasicOutput; |