Don't attempt to recover from undefined behavior in some cases
[emacs.git] / lisp / emacs-lisp / benchmark.el
bloba2217d2095354ff06a6d033d3a4f4346c006e7ca
1 ;;; benchmark.el --- support for benchmarking code -*- lexical-binding: t -*-
3 ;; Copyright (C) 2003-2017 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: lisp, extensions
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Utilities for timing the execution of forms, including the time
26 ;; taken for GC. Note that prior to timing code you may want to
27 ;; ensure things like: there has just been a GC, the relevant code is
28 ;; already loaded (so that there's no overhead from autoloading etc.),
29 ;; and the code is compiled if appropriate (but see
30 ;; `benchmark-run-compiled').
32 ;;; Code:
34 (defmacro benchmark-elapse (&rest forms)
35 "Return the time in seconds elapsed for execution of FORMS."
36 (declare (indent 0) (debug t))
37 (let ((t1 (make-symbol "t1"))
38 (t2 (make-symbol "t2")))
39 `(let (,t1 ,t2)
40 (setq ,t1 (current-time))
41 ,@forms
42 (setq ,t2 (current-time))
43 (float-time (time-subtract ,t2 ,t1)))))
45 ;;;###autoload
46 (defmacro benchmark-run (&optional repetitions &rest forms)
47 "Time execution of FORMS.
48 If REPETITIONS is supplied as a number, run forms that many times,
49 accounting for the overhead of the resulting loop. Otherwise run
50 FORMS once.
51 Return a list of the total elapsed time for execution, the number of
52 garbage collections that ran, and the time taken by garbage collection.
53 See also `benchmark-run-compiled'."
54 (declare (indent 1) (debug t))
55 (unless (natnump repetitions)
56 (setq forms (cons repetitions forms)
57 repetitions 1))
58 (let ((i (make-symbol "i"))
59 (gcs (make-symbol "gcs"))
60 (gc (make-symbol "gc")))
61 `(let ((,gc gc-elapsed)
62 (,gcs gcs-done))
63 (list ,(if (> repetitions 1)
64 ;; Take account of the loop overhead.
65 `(- (benchmark-elapse (dotimes (,i ,repetitions)
66 ,@forms))
67 (benchmark-elapse (dotimes (,i ,repetitions))))
68 `(benchmark-elapse ,@forms))
69 (- gcs-done ,gcs)
70 (- gc-elapsed ,gc)))))
72 ;;;###autoload
73 (defmacro benchmark-run-compiled (&optional repetitions &rest forms)
74 "Time execution of compiled version of FORMS.
75 This is like `benchmark-run', but what is timed is a funcall of the
76 byte code obtained by wrapping FORMS in a `lambda' and compiling the
77 result. The overhead of the `lambda's is accounted for."
78 (declare (indent 1) (debug t))
79 (unless (natnump repetitions)
80 (setq forms (cons repetitions forms)
81 repetitions 1))
82 (let ((i (make-symbol "i"))
83 (gcs (make-symbol "gcs"))
84 (gc (make-symbol "gc"))
85 (code (byte-compile `(lambda () ,@forms)))
86 (lambda-code (byte-compile `(lambda ()))))
87 `(let ((,gc gc-elapsed)
88 (,gcs gcs-done))
89 (list ,(if (> repetitions 1)
90 ;; Take account of the loop overhead.
91 `(- (benchmark-elapse (dotimes (,i ,repetitions)
92 (funcall ,code)))
93 (benchmark-elapse (dotimes (,i ,repetitions)
94 (funcall ,lambda-code))))
95 `(benchmark-elapse (funcall ,code)))
96 (- gcs-done ,gcs) (- gc-elapsed ,gc)))))
98 ;;;###autoload
99 (defun benchmark (repetitions form)
100 "Print the time taken for REPETITIONS executions of FORM.
101 Interactively, REPETITIONS is taken from the prefix arg.
102 For non-interactive use see also `benchmark-run' and
103 `benchmark-run-compiled'."
104 (interactive "p\nxForm: ")
105 (let ((result (eval `(benchmark-run ,repetitions ,form))))
106 (if (zerop (nth 1 result))
107 (message "Elapsed time: %fs" (car result))
108 (message "Elapsed time: %fs (%fs in %d GCs)" (car result)
109 (nth 2 result) (nth 1 result)))))
111 (provide 'benchmark)
113 ;;; benchmark.el ends here