home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / lisp / mcl / 2064 < prev    next >
Encoding:
Text File  |  1993-01-21  |  3.2 KB  |  95 lines

  1. Path: sparky!uunet!usc!cs.utexas.edu!sun-barr!ames!data.nas.nasa.gov!taligent!apple!cambridge.apple.com!@explorer.dgp.toronto.edu:markt@dgp.toronto.edu
  2. From: markt@dgp.toronto.edu ("Mark A. Tapia")
  3. Newsgroups: comp.lang.lisp.mcl
  4. Subject: Re:  Measuring elapsed CPU time
  5. Message-ID: <93Jan21.093724est.144034@explorer.dgp.toronto.edu>
  6. Date: 21 Jan 93 14:37:15 GMT
  7. Sender: owner-info-mcl@cambridge.apple.com
  8. Lines: 84
  9. Approved: comp.lang.lisp.mcl@Cambridge.Apple.C0M
  10.  
  11. On Thu Jan 21 05:07:05 1993, Joung-woo John Kim (joungwoo@mensa.usc.edu)
  12. writes:
  13.    I'm trying to measure teh elasped CPU time during a lisp form has been run.
  14.    ...
  15.    So I tried using builtin function GET-INTERNAL-RUN-TIME as follows,
  16.    but the following function defined by me usually it gives me either 0 or 1 no
  17.    matter how time-consuming task I give it.
  18. John then describes a function of the form:
  19.    (defun CPU-time (form)
  20.    ...
  21.    )
  22. The problem is that the form is evaluated before the body of the
  23. CPU-time function is invoked. If you want to time a function,
  24. you need to convert your function to a macro. The following
  25. macro does what you want
  26.  
  27.  
  28.         
  29. ;; monitoring routines for timing
  30. ;; execute the form ntimes and give the title to it
  31. (defpackage :timing (:use "COMMON-LISP" "CCL"))
  32. (in-package :timing)
  33.  
  34. ;; Execute form ntimes, optionally garbage collect (:gc t),
  35. ;; optionally prints the statistics (:print t), and
  36. ;; returns the following values and optionally prints them
  37. ;;     title
  38. ;;     number of times the form was executed
  39. ;;     total cpu time
  40. ;;     cpu time less gc time
  41. ;;     total gc time
  42. ;;     ephemeral gc time
  43. (defmacro form-stats (title form  &key (ntimes 1) (print t) (gc nil))
  44.   (let ((var1 (gensym))
  45.         (tot (gensym))
  46.         (run (gensym))
  47.         (values (gensym))
  48.         (tot-gc (gensym))
  49.         (new-gc (gensym))
  50.         (egc (gensym))
  51.         (new-egc (gensym)))
  52.     `(let (,tot ,run ,values)
  53.        (when ,gc
  54.          (gc))
  55.        (ccl::reset-consing)
  56.        (multiple-value-bind (,tot-gc ,egc) 
  57.                             (gctime)
  58.          (setq ,egc (- ,tot-gc ,egc))
  59.          (setq ,run (get-internal-run-time))
  60.          (dotimes (,var1 ,ntimes)
  61.            ,form)
  62.          (multiple-value-bind (,new-gc ,new-egc)
  63.                               (gctime)
  64.            (setq ,new-egc (- ,new-gc ,new-egc))
  65.            (setq ,tot-gc (- ,new-gc ,tot-gc)
  66.                  ,egc (- ,new-egc  ,egc)
  67.                  ,tot (- (get-internal-run-time) , run)
  68.                  ,run (- ,tot ,tot-gc) 
  69.                  ,run (float (/ ,run internal-time-units-per-second))
  70.                  ,tot-gc (float (/ ,tot-gc internal-time-units-per-second))
  71.                  ,egc (float (/ ,egc internal-time-units-per-second))
  72.                  ,tot (float (/ ,tot ,internal-time-units-per-second)))
  73.            (setq ,values
  74.                  (format nil "~s ~d ~3f ~3f ~3f ~3f",title ,ntimes ,tot ,run  
  75.                          ,tot-gc ,egc))
  76.            (when ,print (format t "~&~a~%" ,values))
  77.            (values ,title ,ntimes ,tot ,run ,tot-gc ,egc)
  78.            )))))
  79.   
  80. ? (form-stats "simple loop"
  81.                 (loop for i fixnum from 1 to 10000
  82.                       collect i))
  83. "simple loop" 1 1.1 0.13 1.0 1.0
  84. "simple loop"
  85. 1
  86. 1.15
  87. 0.133
  88. 1.017
  89. 1.017
  90.  
  91.  
  92. mark
  93.  
  94. You can also collect statistics on storage used.
  95.