5.3.3 The performance monitor
;; The function MAKE-EXAMPLE-LIST creates a list of numbers in (defun make-example-list (count) (let (example-list) (dotimes (i count example-list) (push i example-list)))) ;;; The function EXAMPLE-FUNCTION constructs a palindromic list ;;; of numbers the length of the specified argument. It does not ;;; use an efficient algorithm for its computation. (defun example-function (count) (let ((list (make-example-list count))) (dotimes (i (length list)) (let ((obj (nth i list))) (when (example-test-p i obj) (setf (nth i list) i)))))) ;;; The function EXAMPLE-FUNCTION-2 constructs a palindromic list ;;; of numbers the length of the specified argument. (defun example-function-2 (count) (do* ((list (make-example-list count) (cdr list)) (i 0 (1+ i)) (obj (car list) (car list))) ((null list) nil) (when (example-test-p i obj) (setf (car list) i)))) ;;; The function EXAMPLE-TEST-P compares two numbers. (defun example-test-p (index object) (< index object))The results returned by the Performance Monitor show that example-function is much less efficient than example-function-2; the algorithm for example-function has a computing time that is on the order of (n2), where n is the size of its argument:
> (monitor example-function example-function-2) (EXAMPLE-FUNCTION EXAMPLE-FUNCTION-2) > (start-monitoring) T > (example-function 1000) NIL > (example-function-2 1000) NIL > (stop-monitoring) NIL > (print-monitor 'example-function) Monitor information for EXAMPLE-FUNCTION Number of calls : 1 Inclusive Time (secs) : 0.398 Exclusive Time (secs) : 0.398 Inclusive Consing (words) : 2004 Exclusive Consing (words) : 2004 NIL > (print-monitor 'example-function-2) Monitor information for EXAMPLE-FUNCTION-2 Number of calls : 1 Inclusive Time (secs) : 0.019 Exclusive Time (secs) : 0.019 Inclusive Consing (words) : 2000 Exclusive Consing (words) : 2000 NILThe Performance Monitor returns the following information:
;;; SUMMARIZE-MONITORS prints a summary of the monitor information ;;; that has been collected. The keyword argument :NUMBER-OF-CALLS ;;; is set to T to ensure that the number of calls are part of the ;;; summary; the keyword arguments :EXCLUSIVE-TIME and ;;; :EXCLUSIVE-CONSING are T by default. > (summarize-monitors :number-of-calls t) Cumulative Exclusive Time (Seconds) 1: EXAMPLE-FUNCTION -- 0.398 2: EXAMPLE-FUNCTION-2 -- 0.019 Cumulative Exclusive Consing (Words) 1: EXAMPLE-FUNCTION -- 2000 2: EXAMPLE-FUNCTION-2 -- 2004 Cumulative Total Calls 1: LIQUID:SET-CURRENT-AREA -- 47 2: EXAMPLE-FUNCTION -- 1 3: EXAMPLE-FUNCTION-2 -- 1 NILNote that the summary includes information about some internal functions of the implementation, such as liquid:set-current-area.
If the amount of run-time or storage used by a function seems unreasonable, you have found a place where rewriting might make a difference.
Generated with Harlequin WebMaker