<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi<div><br></div><div>I added a second profiler report with hierarchical details, any suggestions for</div><div>improvements or corrections are welcome:</div><div><br></div><div><div>(defmacro with-profiler (syms &rest body)</div><div> `(let ((*profile-hash-1* (make-hash-table))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (*profile-hash-2* (make-hash-table :test #'equal))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (*profile-stack* nil))</div><div> (flet ((before-trace (f &rest vals)</div><div> (declare (ignore vals))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (let ((tim (get-real-time)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(multiple-value-bind (last count elapsed) (values-list (gethash f *profile-hash-1*))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (declare (ignore last))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (setf (gethash f *profile-hash-1*) (list tim (+ 1 count) elapsed)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(push f *profile-stack*)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(multiple-value-bind (last count elapsed) (values-list (gethash *profile-stack* *profile-hash-2*))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (if (null last) (setf elapsed 0 count 0))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (setf (gethash *profile-stack* *profile-hash-2*) (list tim (+ 1 count) elapsed)))))</div><div> (after-trace (f &rest vals)</div><div> (declare (ignore vals))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (let ((tim (get-real-time)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(multiple-value-bind (last count elapsed) (values-list (gethash f *profile-hash-1*))</div><div> (setf (gethash f *profile-hash-1*) (list tim count (+ elapsed (- tim last)))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(multiple-value-bind (last count elapsed) (values-list (gethash *profile-stack* *profile-hash-2*))</div><div> (setf (gethash *profile-stack* *profile-hash-2*) (list tim count (+ elapsed (- tim last)))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(pop *profile-stack*))))</div><div> (dolist (s ',syms)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (format t "profiling ~S~%" s) (force-output t)</div><div> (setf (gethash s *profile-hash-1*) '(nil 0 0))</div><div> (CCL::%UNTRACE-0 (list s))</div><div> (CCL::%TRACE-0 (list (list s :before #'before-trace :after #'after-trace))</div><div> 'NIL))</div><div> (format t "~%profiling ~A~%" (trace))</div><div> (let ((total-elapsed (get-real-time)) (lst nil) (nano 1000000000))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (eval '(progn ,@body))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (setf total-elapsed (- (get-real-time) total-elapsed))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (format t "total elapsed time ~10,3F seconds~%" (/ total-elapsed nano))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (dolist (s ',syms)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (CCL::%UNTRACE-0 (list s)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (maphash #'(lambda (k v) </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (if (> (second v) 0)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (push (list k (second v) (third v)) lst)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> *profile-hash-1*)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (format t "~35A ~8A ~14A~%" "Function" "Count" "Elapsed")</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (dolist (e (sort lst #'(lambda (e f) (< (third e) (third f)))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (format t "~30S ~10d ~10,3F ~5,2F%~%"</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (first e)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (second e)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (/ (third e) nano) </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (* 100 (/ (third e) (if (= 0 total-elapsed) 1 total-elapsed)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> ))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (format t "~%total elapsed time ~10,3F seconds~%" (/ total-elapsed nano))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (setf lst nil)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (maphash #'(lambda (k v) </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (if (> (second v) 0)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (push (list (reverse k) (second v) (third v)) lst)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> *profile-hash-2*)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (labels ((compare-list (l1 l2)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (if (null l1)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(if (null l2)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> nil</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (if (string< (string (car l1)) (string (car l2)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(if (string> (string (car l1)) (string (car l2)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> nil</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (compare-list (cdr l1) (cdr l2))))))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (let ((prefix "") (times (make-hash-table)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (setf (gethash 0 times) total-elapsed) ; level 0</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (dolist (e (sort lst #'(lambda (e f) (compare-list (first e) (first f)))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (setf (gethash (length (first e)) times) (third e))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (setf prefix (make-string (* 2 (length (first e))) :initial-element #\.))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (let ((elapsed (gethash (- (length (first e)) 1) times))) ; elapsed time of previous level</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (format t "~A~S ~10,3F ~5,2F%~%" prefix</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (car (last (first e)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (/ (third e) nano) </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (* 100 (/ (third e) (if (= 0 elapsed) 1 elapsed)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> )))))))))</div><div><br></div><div><br></div><div>Example of reports:</div><div><br></div><div><div>total elapsed time 0.847 seconds</div><div>Function Count Elapsed </div><div>%LIST 4 0.000 0.01%</div><div>CENTER-LAYOUT 4 0.000 0.01%</div><div>DESTROY-CONTEXT 1 0.000 0.06%</div><div>%GET-VFIELDS 1 0.001 0.08%</div><div>XCREATEWINDOW 69 0.001 0.09%</div><div>%EVAL 18 0.001 0.11%</div><div>%LIST-SYMBOLS 12 0.001 0.11%</div><div>%GEN-FLET-VFIELDS 4 0.001 0.17%</div><div>%FLET-VFIELDS 1 0.001 0.18%</div><div>%VFIELDS 1 0.002 0.18%</div><div>COLUMN-LAYOUT 11 0.002 0.27%</div><div>RESIZE-WINDOW 69 0.002 0.29%</div><div>%SPLIT-ARGS 272 0.003 0.36%</div><div>ROW-LAYOUT 16 0.003 0.37%</div><div>SPLIT-ARGS 272 0.010 1.13%</div><div>CLIP 27 0.013 1.52%</div><div>GETA 2479 0.027 3.14%</div><div>CREATE-WINDOW 69 0.037 4.41%</div><div>CREATE-CONTEXT 69 0.046 5.43%</div><div><br></div><div>total elapsed time 0.847 seconds</div><div>..%EVAL 0.001 0.13%</div><div>....%EVAL 0.001 97.22%</div><div>......%EVAL 0.001 88.97%</div><div>........%EVAL 0.001 80.29%</div><div>..........%EVAL 0.000 1.62%</div><div>..........%GET-VFIELDS 0.001 83.75%</div><div>............GETA 0.000 7.86%</div><div>............SPLIT-ARGS 0.000 60.71%</div><div>..............%SPLIT-ARGS 0.000 33.20%</div><div>..%VFIELDS 0.002 0.18%</div><div>....%FLET-VFIELDS 0.001 98.27%</div><div>......%GEN-FLET-VFIELDS 0.001 95.39%</div><div>........%LIST 0.000 3.62%</div><div>........%LIST-SYMBOLS 0.001 67.14%</div><div>..CENTER-LAYOUT 0.000 0.01%</div><div>..CLIP 0.013 1.52%</div><div>....GETA 0.001 6.60%</div><div>....RESIZE-WINDOW 0.001 8.26%</div><div>..COLUMN-LAYOUT 0.002 0.27%</div><div>....GETA 0.000 9.94%</div><div>..CREATE-CONTEXT 0.046 5.43%</div><div>....CREATE-WINDOW 0.037 81.25%</div><div>......GETA 0.016 43.95%</div><div>......XCREATEWINDOW 0.001 2.06%</div><div>....GETA 0.001 3.10%</div><div>..DESTROY-CONTEXT 0.000 0.06%</div><div>..GETA 0.007 0.86%</div><div>..RESIZE-WINDOW 0.001 0.16%</div><div>..ROW-LAYOUT 0.003 0.37%</div><div>....GETA 0.000 10.53%</div><div>..SPLIT-ARGS 0.009 1.08%</div><div>....%SPLIT-ARGS 0.003 31.65%</div><div>NIL</div><div>? </div><div><br></div></div><div><br></div></div><div><br><div><div>On Nov 19, 2009, at 7:41 PM, Paul Krueger wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Taoufik,<div><br></div><div>I really like this idea, but unfortunately the code as written didn't work for me.</div><div><br></div><div>I first created a couple of test functions:</div><div><br></div><div><div>(defun test-factorial (n)</div><div> (if (<= n 1) </div><div> 1</div><div> (* n (test-factorial (1- n)))))</div><div><br></div><div>(defun test-profile ()</div><div> (with-profiler (test-factorial)</div><div> (dotimes (i 1000)</div><div> (test-factorial (random 50)))))</div><div><br></div><div>but on executing the test-profile function the macro-expansion caused the following:</div><div><br></div><div><div>(test-profile)</div><div>;Compiler warnings :</div><div>; In an anonymous lambda form inside (CCL::TRACED TEST-FACTORIAL): Undeclared free variable *PROFILER* (2 references)</div><div>; In an anonymous lambda form inside (CCL::TRACED TEST-FACTORIAL): Undeclared free variable *PROFILER* (2 references)</div><div>profiling (TEST-FACTORIAL)</div><div>> Error: Unbound variable: *PROFILER*</div><div>> While executing: (:INTERNAL (CCL::TRACED TEST-FACTORIAL)), in process Listener(6).</div><div>> Type cmd-. to abort, cmd-\ for a list of available restarts.</div><div>> Type :? for other options.</div><div>1 > </div><div><br></div><div>This is caused by the use of the ` form for the trace-0 argument calls which effectively hides the let declaration of *profiler* because you are passing a list that contains the unevaluated lambda form to trace rather than passing a list that contains the function that results from evaluation of that form within the scope of the let. Even if we fixed just that problem, this code would also create new anonymous :before and :after functions for each symbol being profiled when in fact you only need one for each hash-table.</div></div></div><div><br></div><div>So I modified your code a bit to define the functions just once and to eliminate some of the ` forms which caused problems:</div><div><br></div><div><div>(defmacro with-profiler (syms &rest body)</div><div> `(let ((*profile-hash* (make-hash-table :test #'equal)))</div><div> (flet ((before-trace (f &rest vals)</div><div> (declare (ignore vals))</div><div> (multiple-value-bind (last count elapsed) (values-list (gethash f *profile-hash*))</div><div> (declare (ignore last))</div><div> (setf (gethash f *profile-hash*) (list (get-real-time) (+ 1 count) elapsed))))</div><div> (after-trace (f &rest vals)</div><div> (declare (ignore vals))</div><div> (multiple-value-bind (last count elapsed) (values-list (gethash f *profile-hash*))</div><div> (let ((tim (get-real-time)))</div><div> (setf (gethash f *profile-hash*) (list tim count (+ elapsed (- tim last))))))))</div><div> (dolist (s ',syms)</div><div> (setf (gethash s *profile-hash*) '(nil 0 0))</div><div> (CCL::%UNTRACE-0 (list s))</div><div> (CCL::%TRACE-0 (list (list s :before #'before-trace :after #'after-trace))</div><div> 'NIL))</div><div> (format t "~%profiling ~A~%" (trace))</div><div> (let ((elapsed (get-real-time)))</div><div> (eval '(progn ,@body))</div><div> (format t "total elapsed time ~10,3F seconds~%" (/ (- (get-real-time) elapsed) 1000000000)))</div><div> (dolist (s ',syms)</div><div> (CCL::%UNTRACE-0 (list s))</div><div> (let ((lst nil) (total 0))</div><div> (maphash #'(lambda (k v) </div><div> (incf total (/ (third v) 1000000000))</div><div> (push (list k (second v) (third v)) lst))</div><div> *profile-hash*)</div><div> (format t "~35A ~8A ~14A~%" "Function" "Count" "Elapsed")</div><div> (dolist (e (sort lst #'(lambda (e f) (< (third e) (third f)))))</div><div> (format t "~30S ~10d ~10,3F ~5,2F%~%"</div><div> (first e) (second e) (/ (third e) 1000000000) (* 100 (/ (/ (third e) 1000000000) total)))))))))</div><div><br></div><div>Using this code everything worked fine:</div><div><br></div><div><div>? (test-profile)</div><div><br></div><div>profiling (TEST-FACTORIAL)</div><div>total elapsed time 0.021 seconds</div><div>Function Count Elapsed </div><div>TEST-FACTORIAL 1000 0.011 100.00%</div><div>NIL</div><div><br></div></div><div><br></div><div>Regards,</div><div><br></div><div>Paul</div><div><br></div><div><br></div></div><div><div><div>On Nov 19, 2009, at 9:54 AM, Taoufik Dachraoui wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">The elapsed time is in nanoseconds; in the new version of with-profiler the times are in<div>seconds.<div><br></div><div>I modified the with-profiler macro to sort the elapsed times and added a column to show the percentage of the elapsed times :</div><div><br></div><div><div>(defmacro with-profiler (syms &rest body)</div><div> `(let ((*profiler* (make-hash-table :test #'equal)))</div><div> (dolist (s ',syms)</div><div> (setf (gethash `,s *profiler*) '(nil 0 0))</div><div> (CCL::%UNTRACE-0 `(,s))</div><div> (CCL::%TRACE-0 `((,s :before #'(lambda (f &rest vals)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (declare (ignore vals))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (multiple-value-bind (last count elapsed) (values-list (gethash f *profiler*))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (declare (ignore last))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (setf (gethash f *profiler*) (list (get-real-time) (+ 1 count) elapsed))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> :after #'(lambda (f &rest vals)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (declare (ignore vals))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (multiple-value-bind (last count elapsed) (values-list (gethash f *profiler*))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (let ((tim (get-real-time)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (setf (gethash f *profiler*) (list tim count (+ elapsed (- tim last)))))))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> 'NIL))</div><div> (format t "~%profiling ~A~%" (trace))</div><div> (let ((elapsed (get-real-time)))</div><div> (eval '(progn ,@body))</div><div> (format t "total elapsed time ~10,3F seconds~%" (/ (- (get-real-time) elapsed) 1000000000)))</div><div> (dolist (s ',syms)</div><div> (CCL::%UNTRACE-0 `(,s)))</div><div> (let ((lst nil) (total 0))</div><div> (maphash #'(lambda (k v) </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (incf total (/ (third v) 1000000000))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (push (list k (second v) (third v)) lst))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> *profiler*)</div><div> (format t "~35A ~8A ~14A~%" "Function" "Count" "Elapsed")</div><div> (dolist (e (sort lst #'(lambda (e f) (< (third e) (third f)))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(format t "~30S ~10d ~10,3F ~5,2F%~%"</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(first e) (second e) (/ (third e) 1000000000) (* 100 (/ (/ (third e) 1000000000) total)))))</div><div> ))</div><div><br></div><div>Output example:</div><div><br></div><div><div>total elapsed time 1.159 seconds</div><div>Function Count Elapsed </div><div>SEND-MESSAGE-TO-WINDOW 0 0.000 0.00</div><div>LEFT-LAYOUT 0 0.000 0.00</div><div>RROW-LAYOUT 0 0.000 0.00</div><div>RIGHT-LAYOUT 0 0.000 0.00</div><div>RCOLUMN-LAYOUT 0 0.000 0.00</div><div>GET-EVENT-HANDLER 0 0.000 0.00</div><div>%LIST 4 0.000 0.01</div><div>CENTER-LAYOUT 4 0.000 0.01</div><div>%LIST-SYMBOLS 12 0.000 0.03</div><div>%GET-VFIELDS 1 0.000 0.06</div><div>%GEN-FLET-VFIELDS 4 0.001 0.07</div><div>%FLET-VFIELDS 1 0.001 0.08</div><div>%VFIELDS 1 0.001 0.08</div><div>DESTROY-CONTEXT 1 0.001 0.08</div><div>XCREATEWINDOW 69 0.001 0.09</div><div>%EVAL 20 0.001 0.09</div><div>%SPLIT-ARGS 254 0.003 0.34</div><div>RESIZE-WINDOW 69 0.003 0.37</div><div>COLUMN-LAYOUT 11 0.005 0.65</div><div>SPLIT-ARGS 254 0.008 1.05</div><div>ROW-LAYOUT 16 0.008 1.13</div><div>CLIP 27 0.023 3.06</div><div>GETA 19185 0.172 23.17</div><div>CREATE-WINDOW 69 0.253 34.02</div><div>CREATE-CONTEXT 69 0.264 35.60</div><div>NIL</div><div>?</div><div><br></div></div><div>Taoufik</div><div><div>On Nov 19, 2009, at 4:44 PM, Alexander Repenning wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">looks great to me. It seems to use similar functions as the timer code I pointed to in my previous post but includes a nice profilers wrap up macro. I suggest to use time formatting functions similar to the code I pointed to because it is not clear what "elapsed=268738" really means. Just a detail.<div><br></div><div>best, Alex</div><div><br><div><div>On Nov 19, 2009, at 7:24 AM, Taoufik Dachraoui wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Hi,<br><br>I created a macro with-profiler and would like to know the <br>disadvantages of this solution<br>compared to others:<br><br>; i took the following 2 functions from ccl-1.4/tools/advice-profiler/ <br>profiler.lisp<br>; the with-profiler can be modified to return cpu-time, user-time and <br>real-time used<br>; by all profiled functions<br><br>(defun mach-timespec->nanoseconds (ts)<br> "Convert the given typespec structure into nanoseconds."<br> (+ (* 1000000000 (pref ts :mach_timespec.tv_sec))<br> (pref ts :mach_timespec.tv_nsec)))<br><br>(defun get-real-time ()<br> (let ((clock-port (make-record :clock_serv_t)))<br> (#_host_get_clock_service (#_mach_host_self) #$REALTIME_CLOCK <br>clock-port)<br> (ccl:rlet ((ts :mach_timespec))<br><span class="Apple-tab-span" style="white-space:pre"> </span> (#_clock_get_time (%get-ptr clock-port) ts)<br><span class="Apple-tab-span" style="white-space:pre"> </span> (mach-timespec->nanoseconds ts))))<br><br>(defmacro with-profiler (syms &rest body)<br> `(let ((*profiler* (make-hash-table :test #'equal)))<br> (dolist (s ',syms)<br> (setf (gethash `,s *profiler*) '(nil 0 0))<br> (CCL::%UNTRACE-0 `(,s))<br> (CCL::%TRACE-0 `((,s :before #'(lambda (f &rest vals)<br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> (declare (ignore vals))<br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> (multiple-value-bind (last count elapsed) (values-list <br>(gethash f *profiler*))<br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> (declare (ignore last))<br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> (setf (gethash f *profiler*) (list (get-real-time) (+ 1 count) <br>elapsed))))<br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> :after #'(lambda (f &rest vals)<br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> (declare (ignore vals))<br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> (multiple-value-bind (last count elapsed) (values-list <br>(gethash f *profiler*))<br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> (let ((tim (get-real-time)))<br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> (setf (gethash f *profiler*) (list tim count (+ elapsed (- tim <br>last)))))))))<br><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span> 'NIL))<br> (format t "~%profiling ~A~%" (trace))<br> (let ((elapsed (get-real-time)))<br> (eval '(progn ,@body))<br> (format t "total elapsed time ~S~%" (- (get-real-time) elapsed)))<br> (dolist (s ',syms)<br> (CCL::%UNTRACE-0 `(,s)))<br> (maphash #'(lambda (k v) (format t "function=~S count=~S <br>elapsed=~S~%" k (second v) (third v))) *profiler*)))<br><br>Tests:<br><br>? (defun fact (n) (if (= 0 n) 1 (* n (fact (- n 1)))))<br>? (defun f () (sleep 1))<br>? (defmacro foo () (progn (f) ''foo))<br>? (with-profiler (f fact)<br><span class="Apple-tab-span" style="white-space:pre"> </span>(dotimes (i 10)<br><span class="Apple-tab-span" style="white-space:pre"> </span> (f)<br><span class="Apple-tab-span" style="white-space:pre"> </span> (foo)<br><span class="Apple-tab-span" style="white-space:pre"> </span> (fact (random 100))))<br><br>profiling (FACT F)<br>total elapsed time 11003452329<br>function=FACT count=10 elapsed=268738<br>function=F count=11 elapsed=11001194423<br>NIL<br>?<br><br>Any comments on this method of profiling lisp functions are welcome<br><br>Kind regards<br>Taoufik<br><br>On Nov 18, 2009, at 5:57 PM, Matt Tenenbaum wrote:<br><br><blockquote type="cite"><br></blockquote><blockquote type="cite">Norvig's classic "Paradigms of Artificial Intelligence Programming"<br></blockquote><blockquote type="cite">has a section on instrumentation in chapter 9, including code for<br></blockquote><blockquote type="cite">profiling in a manner analogous to TRACE. In looking quickly, this<br></blockquote><blockquote type="cite">code doesn't seem to be included in the distributed source for the<br></blockquote><blockquote type="cite">book (<a href="http://norvig.com/paip/README.html">http://norvig.com/paip/README.html</a>), but you might find that<br></blockquote><blockquote type="cite">chapter helpful.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Cheers,<br></blockquote><blockquote type="cite">-mt<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">On Wed, Nov 18, 2009 at 8:36 AM, Taoufik Dachraoui<br></blockquote><blockquote type="cite"><<a href="mailto:taoufik.dachraoui@wanadoo.fr">taoufik.dachraoui@wanadoo.fr</a>> wrote:<br></blockquote><blockquote type="cite"><blockquote type="cite">Hi<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">How do I profile lisp program to know how much time each called<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">function takes<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">Regards<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">Taoufik<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">_______________________________________________<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite">Openmcl-devel mailing list<br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><a href="mailto:Openmcl-devel@clozure.com">Openmcl-devel@clozure.com</a><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><a href="http://clozure.com/mailman/listinfo/openmcl-devel">http://clozure.com/mailman/listinfo/openmcl-devel</a><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><br></blockquote></blockquote><blockquote type="cite"><br></blockquote><br><br><br>_______________________________________________<br>Openmcl-devel mailing list<br><a href="mailto:Openmcl-devel@clozure.com">Openmcl-devel@clozure.com</a><br><a href="http://clozure.com/mailman/listinfo/openmcl-devel">http://clozure.com/mailman/listinfo/openmcl-devel</a><br></div></blockquote></div><br><div> <span class="Apple-style-span" style="font-size: 12px; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" style="font: 12.0px Helvetica">Prof. Alexander Repenning</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br class="khtml-block-placeholder"></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">University of Colorado</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Computer Science Department</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Boulder, CO 80309-430</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br class="khtml-block-placeholder"></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Helvetica" size="3" style="font: 12.0px Helvetica">vCard: <a href="http://www.cs.colorado.edu/~ralex/AlexanderRepenning.vcf">http://www.cs.colorado.edu/~ralex/AlexanderRepenning.vcf</a></font></div><br class="Apple-interchange-newline"></span> </div> <br></div></div></blockquote></div><br></div></div></div>_______________________________________________<br>Openmcl-devel mailing list<br><a href="mailto:Openmcl-devel@clozure.com">Openmcl-devel@clozure.com</a><br><a href="http://clozure.com/mailman/listinfo/openmcl-devel">http://clozure.com/mailman/listinfo/openmcl-devel</a><br></blockquote></div><br></div></div></blockquote></div><br></div></body></html>