profile.lisp

clispでも使えるプロファイラ、profile.lispの使い方。コメントを読むと、Peter Norvig氏が"Paradigms of Artificial Intelligence: Case Studies in Common Lisp"で書いたらしい。

基本的な使い方

profile.lispをloadしてやって、with-profilingで時間を計測する関数を指定してやる。

;; target.lisp
(load "profile.lisp")
(defun g() (sleep 2))
(defun f() (g))
(with-profiling (f g)
  (f))
$ clisp target.lisp
Total elapsed time: 2.00 seconds.
  Count   Secs Time% Name
      1   2.00   100% G
      1   0.00     0% F

注意

測定している関数にかかった時間が0秒だと、zero division errorになる。まあ、それほど困ったバグでもない。
loadではなくrequireを使うとたまにうまく行かないことがある。必ずしも再現するわけではないのがつらいところ。
with-profilingは関数の差し替えを行っている。なので、以下のプログラムは正しく測定できない。

(load "profile.lisp")
(defun g() (sleep 2))
(defvar h #'g)
(defun f()
  (funcall h))
(with-profiling (g)
  (f))

関数を差し替えるまえに、hにgを代入してしまっているため、ただしくgにかかる時間を測定できない。