[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.23 gauche.time - Measure timings

Module: gauche.time
Provides two simple ways to measure execution time of Scheme code. A macro time, which is convenient for interactive use, and <time-counter> objects which are useful to be embedded in the program.

Macro: time expr expr2 ...
Evaluates expr expr2 ... sequentially, as begin, and returns the result(s) of the last expression. Before returning the value(s), the macro reports the elapsed (real) time and CPU times in the user space and the kernel space to the current error port, much like the bourne shell's time command.

The current version uses sys-gettimeofday (See section 6.21.8.1 POSIX time) to calculate the elapsed time, and sys-times (See section 6.21.7 System Inquiry) to calculate user and system CPU times. So the resolution of these numbers depends on these underlying system calls. Usually the CPU time has 10ms resolution, while the elapsed time might have higher resolution. On the systems that doesn't have gettimeofday(2) support, however, the elapsed time resolution can be as bad as a second.

 
gosh> (time (length (sort (call-with-input-file "/usr/share/dict/words" port->string-list))))
;(time (length (sort (call-with-input-file "/usr/share/dict/words" port- ...
; real   0.357
; user   0.350
; sys    0.000
45427

Class: <time-counter>
An abstract class of time counters. Time counter is a kind of timer whose value is incremented as the time passes. The counting can be started and stopped any number of times. The value of the counter can be read when the timer is stopping. You can have multiple time counters. It is useful, for example, to measure the time in two parts inside a loop independently.

The concrete subclass determines which time it is counting. You have to instantiate one of those subclasses described below to use the time counter.

Class: <real-time-counter>
Class: <user-time-counter>
Class: <system-time-counter>
Class: <process-time-counter>
Classes for time counters that count real (elapsed) time, user-space CPU time, kernel-space CPU time, and total CPU time (user + system), respectively.

Method: time-counter-start! (counter <time-counter>)
Method: time-counter-stop! (counter <time-counter>)
Starts and stops the counter. The time during the counter is running is accumulated to the counter value when the counter is stopped.

Start/stop pairs can be nested, but only the outermost pair takes the effect. That is, if you call time-counter-start! on the counter that is already started, it doesn't have any effect except that to stop such a counter you have to call time-counter-stop! one more time. It is useful when you want to measure the time spent in the larger block that may already contain timer start/stop pairs.

Calling time-counter-stop! on the already stopped counter has no effect.

Method: time-counter-reset! (counter <time-counter>)
Resets the value of counter. If counter is already running, it is forced to stop before being reset.

Method: time-counter-value (counter <time-counter>)
Returns the current value of the counter as the number of seconds, in a real number. The resolution depends on the source of the counter.

Macro: with-time-counter counter expr ...
A convenience macro to run the counter while expr ... are evaluated. Returns the result(s) of the last expression. It is defined as follows.
 
(define-syntax with-time-counter
  (syntax-rules ()
    ((_ counter . exprs)
     (dynamic-wind
      (lambda () (time-counter-start! counter))
      (lambda () . exprs)
      (lambda () (time-counter-stop! counter))))
    ))

The following example measures approximate times spend in process-A and process-B inside a loop.

 
(let ((ta (make <real-time-counter>))
      (tb (make <real-time-counter>)))
  (dotimes (i 100000)
    (with-time-counter ta
      (process-A))
    (with-time-counter tb
      (process-B)))
  (format #t "Time spent in process-A: ~s\n" (time-counter-value ta))
  (format #t "Time spent in process-B: ~s\n" (time-counter-value tb))
  )


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html