annotate lisp/emacs-lisp/benchmark.el @ 88088:985bbb7a574c

emacs-lisp-intro.texi: Update back cover text.
author Robert J. Chassell <bob@rattlesnake.com>
date Thu, 31 Jan 2008 02:24:20 +0000
parents 78ee6fae0e41
children 606f2d163a64 1e3a407766b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49574
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
1 ;;; benchmark.el --- support for benchmarking code
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
2
79704
78ee6fae0e41 Add 2008 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 78217
diff changeset
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
49574
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
4
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
5 ;; Author: Dave Love <fx@gnu.org>
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
6 ;; Keywords: lisp, extensions
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
7
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
8 ;; This file is free software; you can redistribute it and/or modify
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
9 ;; it under the terms of the GNU General Public License as published by
78217
935157c0b596 Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents: 75346
diff changeset
10 ;; the Free Software Foundation; either version 3, or (at your option)
49574
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
11 ;; any later version.
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
12
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
13 ;; This file is distributed in the hope that it will be useful,
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
16 ;; GNU General Public License for more details.
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
17
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
18 ;; You should have received a copy of the GNU General Public License
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
64085
18a818a2ee7c Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 52401
diff changeset
20 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18a818a2ee7c Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 52401
diff changeset
21 ;; Boston, MA 02110-1301, USA.
49574
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
22
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
23 ;;; Commentary:
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
24
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
25 ;; Utilities for timing the execution of forms, including the time
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
26 ;; taken for GC. Note that prior to timing code you may want to
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
27 ;; ensure things like: there has just been a GC, the relevant code is
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
28 ;; already loaded (so that there's no overhead from autoloading etc.),
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
29 ;; and the code is compiled if appropriate (but see
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
30 ;; `benchmark-run-compiled').
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
31
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
32 ;;; Code:
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
33
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
34 (defmacro benchmark-elapse (&rest forms)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
35 "Return the time in seconds elapsed for execution of FORMS."
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
36 (let ((t1 (make-symbol "t1"))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
37 (t2 (make-symbol "t2")))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
38 `(let (,t1 ,t2)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
39 (setq ,t1 (current-time))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
40 ,@forms
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
41 (setq ,t2 (current-time))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
42 (+ (* (- (car ,t2) (car ,t1)) 65536.0)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
43 (- (nth 1 ,t2) (nth 1 ,t1))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
44 (* (- (nth 2 ,t2) (nth 2 ,t1)) 1.0e-6)))))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
45 (put 'benchmark-elapse 'edebug-form-spec t)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
46 (put 'benchmark-elapse 'lisp-indent-function 0)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
47
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
48 ;;;###autoload
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
49 (defmacro benchmark-run (&optional repetitions &rest forms)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
50 "Time execution of FORMS.
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
51 If REPETITIONS is supplied as a number, run forms that many times,
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
52 accounting for the overhead of the resulting loop. Otherwise run
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
53 FORMS once.
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
54 Return a list of the total elapsed time for execution, the number of
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
55 garbage collections that ran, and the time taken by garbage collection.
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
56 See also `benchmark-run-compiled'."
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
57 (unless (natnump repetitions)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
58 (setq forms (cons repetitions forms)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
59 repetitions 1))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
60 (let ((i (make-symbol "i"))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
61 (gcs (make-symbol "gcs"))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
62 (gc (make-symbol "gc")))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
63 `(let ((,gc gc-elapsed)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
64 (,gcs gcs-done))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
65 (list ,(if (> repetitions 1)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
66 ;; Take account of the loop overhead.
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
67 `(- (benchmark-elapse (dotimes (,i ,repetitions)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
68 ,@forms))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
69 (benchmark-elapse (dotimes (,i ,repetitions))))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
70 `(benchmark-elapse ,@forms))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
71 (- gcs-done ,gcs)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
72 (- gc-elapsed ,gc)))))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
73 (put 'benchmark-run 'edebug-form-spec t)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
74 (put 'benchmark-run 'lisp-indent-function 2)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
75
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
76 ;;;###autoload
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
77 (defmacro benchmark-run-compiled (&optional repetitions &rest forms)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
78 "Time execution of compiled version of FORMS.
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
79 This is like `benchmark-run', but what is timed is a funcall of the
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
80 byte code obtained by wrapping FORMS in a `lambda' and compiling the
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
81 result. The overhead of the `lambda's is accounted for."
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
82 (unless (natnump repetitions)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
83 (setq forms (cons repetitions forms)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
84 repetitions 1))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
85 (let ((i (make-symbol "i"))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
86 (gcs (make-symbol "gcs"))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
87 (gc (make-symbol "gc"))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
88 (code (byte-compile `(lambda () ,@forms)))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
89 (lambda-code (byte-compile `(lambda ()))))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
90 `(let ((,gc gc-elapsed)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
91 (,gcs gcs-done))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
92 (list ,(if (> repetitions 1)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
93 ;; Take account of the loop overhead.
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
94 `(- (benchmark-elapse (dotimes (,i ,repetitions)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
95 (funcall ,code)))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
96 (benchmark-elapse (dotimes (,i ,repetitions)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
97 (funcall ,lambda-code))))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
98 `(benchmark-elapse (funcall ,code)))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
99 (- gcs-done ,gcs) (- gc-elapsed ,gc)))))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
100 (put 'benchmark-run-compiled 'edebug-form-spec t)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
101 (put 'benchmark-run-compiled 'lisp-indent-function 2)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
102
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
103 ;;;###autoload
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
104 (defun benchmark (repetitions form)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
105 "Print the time taken for REPETITIONS executions of FORM.
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
106 Interactively, REPETITIONS is taken from the prefix arg. For
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
107 non-interactive use see also `benchmark-run' and
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
108 `benchmark-run-compiled'."
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
109 (interactive "p\nxForm: ")
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
110 (let ((result (eval `(benchmark-run ,repetitions ,form))))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
111 (if (zerop (nth 1 result))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
112 (message "Elapsed time: %fs" (car result))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
113 (message "Elapsed time: %fs (%fs in %d GCs)" (car result)
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
114 (nth 2 result) (nth 1 result)))))
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
115
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
116 (provide 'benchmark)
52401
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 49574
diff changeset
117
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 49574
diff changeset
118 ;;; arch-tag: be570e24-4b51-4784-adf3-fa2b56c31946
49574
e4bf575fd91c *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
119 ;;; benchmark.el ends here