1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-11 16:40:45 -08:00

(profile-functions): No need to assume that the

current buffer is writable.
(profile-print, profile-results): Changed to display number of
calls and average time per call.
(profile-update-function): Update the number of calls.
(profile-a-function): Init the number of calls.
This commit is contained in:
Richard M. Stallman 1996-06-28 07:32:38 +00:00
parent 74d6e31c96
commit 37ae4d5c39

View file

@ -83,7 +83,7 @@
(defvar profile-timer-process nil "Process running the timer.") (defvar profile-timer-process nil "Process running the timer.")
(defvar profile-time-list nil (defvar profile-time-list nil
"List of accumulative time for each profiled function.") "List of cumulative calls and time for each profiled function.")
(defvar profile-init-list nil (defvar profile-init-list nil
"List of entry time for each function. \n\ "List of entry time for each function. \n\
Both how many times invoked and real time of start.") Both how many times invoked and real time of start.")
@ -99,7 +99,7 @@ Both how many times invoked and real time of start.")
(defun profile-functions (&optional flist) (defun profile-functions (&optional flist)
"Profile all the functions listed in `profile-functions-list'.\n\ "Profile all the functions listed in `profile-functions-list'.\n\
With argument FLIST, use the list FLIST instead." With argument FLIST, use the list FLIST instead."
(interactive "*P") (interactive "P")
(if (null flist) (setq flist profile-functions-list)) (if (null flist) (setq flist profile-functions-list))
(mapcar 'profile-a-function flist)) (mapcar 'profile-a-function flist))
@ -115,34 +115,33 @@ With argument FLIST, use the list FLIST instead."
(defun profile-print (entry) (defun profile-print (entry)
"Print one ENTRY (from `profile-time-list')." "Print one ENTRY (from `profile-time-list')."
(let ((time (cdr entry)) str (offset 5)) (let* ((calls (car (cdr entry)))
(insert (format "%s" (car entry)) space) (timec (cdr (cdr entry)))
(move-to-column ref-column) (time (+ (car timec) (/ (cdr timec) (float profile-million))))
(setq str (int-to-string (car time))) (avgtime 0.0))
(insert str) (insert (format (concat "%-"
(if (>= (length str) offset) nil (int-to-string profile-max-fun-name)
(move-to-column ref-column) "s%8d%11d.%06d")
(insert (substring spaces 0 (- offset (length str)))) (car entry) calls (car timec) (cdr timec))
(forward-char (length str))) (if (zerop calls)
(setq str (int-to-string (cdr time))) "\n"
(insert "." (substring "000000" 0 (- 6 (length str))) str "\n"))) (format "%12d.%06d\n"
(truncate (setq avgtime (/ time calls)))
(defconst spaces " ") (truncate (* (- avgtime (ftruncate avgtime))
profile-million))))
)))
(defun profile-results () (defun profile-results ()
"Display profiling results in the buffer `*profile*'. "Display profiling results in the buffer `*profile*'.
\(The buffer name comes from `profile-buffer'.)" \(The buffer name comes from `profile-buffer'.)"
(interactive) (interactive)
(let* ((ref-column (+ 8 profile-max-fun-name))
(space (substring spaces 0 ref-column)))
(switch-to-buffer profile-buffer) (switch-to-buffer profile-buffer)
(erase-buffer) (erase-buffer)
(insert "Function" space) (insert "Function" (make-string (- profile-max-fun-name 6) ? ))
(move-to-column ref-column) (insert " Calls Total time (sec) Avg time per call\n")
(insert "Time (Seconds.Useconds)\n" "========" space ) (insert (make-string profile-max-fun-name ?=) " ")
(move-to-column ref-column) (insert "====== ================ =================\n")
(insert "=======================\n") (mapcar 'profile-print profile-time-list))
(mapcar 'profile-print profile-time-list)))
(defun profile-reset-timer () (defun profile-reset-timer ()
(process-send-string profile-timer-process "z\n")) (process-send-string profile-timer-process "z\n"))
@ -197,12 +196,16 @@ With argument FLIST, use the list FLIST instead."
;; assumes that profile-time contains the current time ;; assumes that profile-time contains the current time
(let ((init-time (profile-find-function fun profile-init-list)) (let ((init-time (profile-find-function fun profile-init-list))
(accum (profile-find-function fun profile-time-list)) (accum (profile-find-function fun profile-time-list))
sec usec) calls time sec usec)
(if (or (null init-time) (if (or (null init-time)
(null accum)) (error "Function %s missing from list" fun)) (null accum)) (error "Function %s missing from list" fun))
(setq calls (car accum))
(setq time (cdr accum))
(setcar init-time (1- (car init-time))) ; pop one level in recursion (setcar init-time (1- (car init-time))) ; pop one level in recursion
(if (not (zerop (car init-time))) (if (not (zerop (car init-time)))
nil ; in some recursion level, do not update accum. time nil ; in some recursion level,
; do not update cumulated time
(setcar accum (1+ calls))
(setq init-time (cdr init-time)) (setq init-time (cdr init-time))
(setq sec (- (car profile-time) (car init-time)) (setq sec (- (car profile-time) (car init-time))
usec (- (cdr profile-time) (cdr init-time))) usec (- (cdr profile-time) (cdr init-time)))
@ -211,11 +214,11 @@ With argument FLIST, use the list FLIST instead."
(if (>= usec 0) nil (if (>= usec 0) nil
(setq usec (+ usec profile-million)) (setq usec (+ usec profile-million))
(setq sec (1- sec))) (setq sec (1- sec)))
(setcar accum (+ sec (car accum))) (setcar time (+ sec (car time)))
(setcdr accum (+ usec (cdr accum))) (setcdr time (+ usec (cdr time)))
(if (< (cdr accum) profile-million) nil (if (< (cdr time) profile-million) nil
(setcar accum (1+ (car accum))) (setcar time (1+ (car time)))
(setcdr accum (- (cdr accum) profile-million))) (setcdr time (- (cdr time) profile-million)))
))) )))
(defun profile-convert-byte-code (function) (defun profile-convert-byte-code (function)
@ -243,7 +246,7 @@ With argument FLIST, use the list FLIST instead."
(if (eq (car def) 'lambda) nil (if (eq (car def) 'lambda) nil
(error "To profile: %s must be a user-defined function" fun)) (error "To profile: %s must be a user-defined function" fun))
(setq profile-time-list ; add a new entry (setq profile-time-list ; add a new entry
(cons (cons fun (cons 0 0)) profile-time-list)) (cons (cons fun (cons 0 (cons 0 0))) profile-time-list))
(setq profile-init-list ; add a new entry (setq profile-init-list ; add a new entry
(cons (cons fun (cons 0 (cons 0 0))) profile-init-list)) (cons (cons fun (cons 0 (cons 0 0))) profile-init-list))
(if (< profile-max-fun-name funlen) (setq profile-max-fun-name funlen)) (if (< profile-max-fun-name funlen) (setq profile-max-fun-name funlen))