time.scm 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. ;;; Commentary:
  18. ;; This module exports a single macro: `time'.
  19. ;; Usage: (time exp)
  20. ;;
  21. ;; Example:
  22. ;; guile> (time (sleep 3))
  23. ;; clock utime stime cutime cstime gctime
  24. ;; 3.01 0.00 0.00 0.00 0.00 0.00
  25. ;; 0
  26. ;;; Code:
  27. (define-module (ice-9 time)
  28. :use-module (ice-9 format)
  29. :export (time))
  30. (define (time-proc proc)
  31. (let* ((gc-start (gc-run-time))
  32. (tms-start (times))
  33. (result (proc))
  34. (tms-end (times))
  35. (gc-end (gc-run-time)))
  36. ;; FIXME: We would probably like format ~f to accept rationals, but
  37. ;; currently it doesn't so we force to a flonum with exact->inexact.
  38. (define (get proc start end)
  39. (exact->inexact (/ (- (proc end) (proc start)) internal-time-units-per-second)))
  40. (display "clock utime stime cutime cstime gctime\n")
  41. (format #t "~5,2F ~5,2F ~5,2F ~6,2F ~6,2F ~6,2F\n"
  42. (get tms:clock tms-start tms-end)
  43. (get tms:utime tms-start tms-end)
  44. (get tms:stime tms-start tms-end)
  45. (get tms:cutime tms-start tms-end)
  46. (get tms:cstime tms-start tms-end)
  47. (get identity gc-start gc-end))
  48. result))
  49. (define-macro (time exp)
  50. `(,time-proc (lambda () ,exp)))
  51. ;;; time.scm ends here