home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / runtime / sysclk.scm < prev    next >
Text File  |  1999-01-02  |  3KB  |  84 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: sysclk.scm,v 14.4 1999/01/02 06:19:10 cph Exp $
  4.  
  5. Copyright (c) 1988, 1989, 1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. |#
  21.  
  22. ;;;; System Clock
  23. ;;; package: (runtime system-clock)
  24.  
  25. (declare (usual-integrations))
  26.  
  27. (define (initialize-package!)
  28.   (reset-system-clock!)
  29.   (add-event-receiver! event:after-restore reset-system-clock!))
  30.  
  31. (define (reset-system-clock!)
  32.   (set! offset-time (process-time-clock))
  33.   (set! non-runtime 0))
  34.  
  35. (define offset-time)
  36. (define non-runtime)
  37.  
  38. (define-integrable process-time-clock
  39.   (ucode-primitive system-clock 0))
  40.  
  41. (define-integrable real-time-clock
  42.   (ucode-primitive real-time-clock 0))
  43.  
  44. (define (system-clock)
  45.   (process->system-time (process-time-clock)))
  46.  
  47. (define (runtime)
  48.   (process->system-time (- (process-time-clock) non-runtime)))
  49.  
  50. (define (increment-non-runtime! ticks)
  51.   (set! non-runtime (+ non-runtime ticks)))
  52.  
  53. (define (measure-interval runtime? thunk)
  54.   (let ((start (process-time-clock)))
  55.     (let ((receiver (thunk (process->system-time start))))
  56.       (let ((end (process-time-clock)))
  57.     (if (not runtime?)
  58.         (increment-non-runtime! (- end start)))
  59.     (receiver (process->system-time end))))))
  60.  
  61. (define (process->system-time ticks)
  62.   (internal-time/ticks->seconds (- ticks offset-time)))
  63.  
  64. (define (internal-time/ticks->seconds ticks)
  65.   (/ (exact->inexact ticks) 1000))
  66.  
  67. (define (internal-time/seconds->ticks seconds)
  68.   (round->exact (* seconds 1000)))
  69.  
  70. (define (with-timings thunk receiver)
  71.   (let ((process-start  (process-time-clock))
  72.     (gc-time-start  non-runtime)
  73.     (real-start     (real-time-clock)))
  74.     (let ((value (thunk)))
  75.       (let ((process-end  (process-time-clock))
  76.         (gc-time-end  non-runtime)
  77.         (real-end     (real-time-clock)))
  78.     (let ((process-time (- process-end process-start))
  79.           (gc-time      (- gc-time-end gc-time-start))
  80.           (real-time    (- real-end real-start)))
  81.       (receiver (- process-time gc-time)
  82.             gc-time
  83.             real-time)
  84.       value)))))