merge from 1.8 branch
[bpt/guile.git] / ice-9 / time.scm
1 ;;;; Copyright (C) 2001, 2004, 2006 Free Software Foundation, Inc.
2 ;;;;
3 ;;;; This library is free software; you can redistribute it and/or
4 ;;;; modify it under the terms of the GNU Lesser General Public
5 ;;;; License as published by the Free Software Foundation; either
6 ;;;; version 2.1 of the License, or (at your option) any later version.
7 ;;;;
8 ;;;; This library is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;;; Lesser General Public License for more details.
12 ;;;;
13 ;;;; You should have received a copy of the GNU Lesser General Public
14 ;;;; License along with this library; if not, write to the Free Software
15 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 ;;;;
17 \f
18 ;;; Commentary:
19
20 ;; This module exports a single macro: `time'.
21 ;; Usage: (time exp)
22 ;;
23 ;; Example:
24 ;; guile> (time (sleep 3))
25 ;; clock utime stime cutime cstime gctime
26 ;; 3.01 0.00 0.00 0.00 0.00 0.00
27 ;; 0
28
29 ;;; Code:
30
31 (define-module (ice-9 time)
32 :use-module (ice-9 format)
33 :export (time))
34
35 (define (time-proc proc)
36 (let* ((gc-start (gc-run-time))
37 (tms-start (times))
38 (result (proc))
39 (tms-end (times))
40 (gc-end (gc-run-time)))
41 ;; FIXME: We would probably like format ~f to accept rationals, but
42 ;; currently it doesn't so we force to a flonum with exact->inexact.
43 (define (get proc start end)
44 (exact->inexact (/ (- (proc end) (proc start)) internal-time-units-per-second)))
45 (display "clock utime stime cutime cstime gctime\n")
46 (format #t "~5,2F ~5,2F ~5,2F ~6,2F ~6,2F ~6,2F\n"
47 (get tms:clock tms-start tms-end)
48 (get tms:utime tms-start tms-end)
49 (get tms:stime tms-start tms-end)
50 (get tms:cutime tms-start tms-end)
51 (get tms:cstime tms-start tms-end)
52 (get identity gc-start gc-end))
53 result))
54
55 (define-macro (time exp)
56 `(,time-proc (lambda () ,exp)))
57
58 ;;; time.scm ends here