define-module for elisp special modules
[bpt/guile.git] / module / ice-9 / time.scm
CommitLineData
cd5fea8d 1;;;; Copyright (C) 2001, 2004, 2006 Free Software Foundation, Inc.
e7d82feb 2;;;;
73be1d9e
MV
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
53befeb7 6;;;; version 3 of the License, or (at your option) any later version.
73be1d9e
MV
7;;;;
8;;;; This library is distributed in the hope that it will be useful,
5e38caf1 9;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
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
92205699 15;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a482f2cc 16;;;;
5e38caf1 17\f
e7d82feb
TTN
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:
5e38caf1
KN
30
31(define-module (ice-9 time)
32 :use-module (ice-9 format)
33 :export (time))
34
3b9e23a7 35(define (time-proc proc)
5e38caf1 36 (let* ((gc-start (gc-run-time))
3b9e23a7
KN
37 (tms-start (times))
38 (result (proc))
39 (tms-end (times))
40 (gc-end (gc-run-time)))
ce08a401
KR
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.
5e38caf1 43 (define (get proc start end)
ce08a401 44 (exact->inexact (/ (- (proc end) (proc start)) internal-time-units-per-second)))
5e38caf1
KN
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"
3b9e23a7
KN
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)
262098e0 52 (get identity gc-start gc-end))
5e38caf1 53 result))
3b9e23a7
KN
54
55(define-macro (time exp)
9d07bb72 56 `((@@ (ice-9 time) time-proc) (lambda () ,exp)))
e7d82feb
TTN
57
58;;; time.scm ends here