2017-03-03 18:26:30 +09:00
|
|
|
;;; benchmark.el --- support for benchmarking code -*- lexical-binding: t -*-
|
2003-02-03 15:00:57 +00:00
|
|
|
|
2018-01-01 00:21:42 -08:00
|
|
|
;; Copyright (C) 2003-2018 Free Software Foundation, Inc.
|
2003-02-03 15:00:57 +00:00
|
|
|
|
|
|
|
;; Author: Dave Love <fx@gnu.org>
|
|
|
|
;; Keywords: lisp, extensions
|
|
|
|
|
2008-05-06 03:21:21 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2003-02-03 15:00:57 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 03:21:21 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
2003-02-03 15:00:57 +00:00
|
|
|
|
2008-05-06 03:21:21 +00:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2003-02-03 15:00:57 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 15:52:52 -07:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2003-02-03 15:00:57 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Utilities for timing the execution of forms, including the time
|
|
|
|
;; taken for GC. Note that prior to timing code you may want to
|
|
|
|
;; ensure things like: there has just been a GC, the relevant code is
|
|
|
|
;; already loaded (so that there's no overhead from autoloading etc.),
|
|
|
|
;; and the code is compiled if appropriate (but see
|
|
|
|
;; `benchmark-run-compiled').
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(defmacro benchmark-elapse (&rest forms)
|
|
|
|
"Return the time in seconds elapsed for execution of FORMS."
|
2017-03-03 18:26:30 +09:00
|
|
|
(declare (indent 0) (debug t))
|
Prefer nil to (current-time) when either works
* doc/misc/gnus.texi (Category Syntax):
* lisp/allout-widgets.el (allout-widgets-post-command-business):
* lisp/cedet/ede/detect.el (ede-detect-qtest):
* lisp/cedet/pulse.el (pulse-momentary-highlight-overlay)
(pulse-tick):
* lisp/cedet/semantic.el (bovinate):
* lisp/cedet/semantic/analyze.el:
(semantic-analyze-current-symbol-default, semantic-adebug-analyze):
* lisp/cedet/semantic/analyze/refs.el (semantic-analyze-current-tag):
* lisp/cedet/semantic/lex.el (semantic-lex-test):
* lisp/cedet/semantic/symref/filter.el:
(semantic-symref-test-count-hits-in-tag):
* lisp/cedet/srecode/dictionary.el (srecode-adebug-dictionary):
* lisp/cedet/srecode/map.el (srecode-adebug-maps):
* lisp/desktop.el (desktop-create-buffer):
* lisp/emacs-lisp/benchmark.el (benchmark-elapse):
* lisp/emacs-lisp/elp.el (elp--make-wrapper):
* lisp/epa.el (epa--show-key):
* lisp/erc/erc.el (erc-lurker-cleanup, erc-lurker-p):
* lisp/gnus/gnus-agent.el (gnus-agent-fetch-articles)
(gnus-agent-expire-group-1, gnus-agent-store-article):
* lisp/gnus/gnus-art.el (article-lapsed-string):
* lisp/gnus/gnus-cloud.el (gnus-cloud-update-newsrc-data)
(gnus-cloud-collect-full-newsrc):
* lisp/gnus/gnus-group.el (gnus-group-timestamp-delta):
* lisp/gnus/gnus-html.el (gnus-html-cache-expired):
* lisp/gnus/gnus-score.el (gnus-score-load-file)
(gnus-decay-scores):
* lisp/gnus/nndiary.el (nndiary-expired-article-p):
* lisp/gnus/nnmail.el (nnmail-expired-article-p):
* lisp/gnus/nnmaildir.el (nnmaildir--scan):
* lisp/gnus/score-mode.el (gnus-score-edit-insert-date):
* lisp/image/gravatar.el (gravatar-cache-expired):
* lisp/net/newst-backend.el (newsticker--image-get)
(newsticker--cache-mark-expired):
* lisp/nxml/rng-maint.el (rng-time-function):
* lisp/org/org-agenda.el (org-agenda-to-appt):
* lisp/org/org-clock.el (org-clock-resolve-clock)
(org-clock-resolve, org-resolve-clocks-if-idle):
* lisp/org/org-colview.el (org-columns-edit-value, org-columns)
(org-columns-compute-all, org-agenda-columns):
* lisp/org/org-element.el (org-element--cache-interrupt-p)
(org-element--cache-sync):
* lisp/org/org-habit.el (org-habit-get-faces)
(org-habit-insert-consistency-graphs):
* lisp/org/org-indent.el (org-indent-add-properties):
* lisp/org/org-timer.el (org-timer-start)
(org-timer-pause-or-continue, org-timer-seconds)
(org-timer-show-remaining-time, org-timer-set-timer):
* lisp/org/org.el (org-babel-load-file, org-current-time)
(org-today, org-auto-repeat-maybe, org-read-date-analyze)
(org-small-year-to-year, org-goto-calendar):
* lisp/org/ox.el (org-export-insert-default-template):
* lisp/time.el (emacs-uptime):
* lisp/type-break.el (type-break-mode, type-break)
(type-break-time-warning-schedule, type-break-check):
* lisp/url/url-cache.el (url-cache-expired):
* lisp/url/url.el (url-retrieve-synchronously):
* test/lisp/char-fold-tests.el (char-fold--speed-test):
* test/manual/cedet/semantic-ia-utest.el:
(semantic-symref-test-count-hits-in-tag):
* test/manual/cedet/semantic-tests.el (semantic-idle-pnf-test)
(semantic-lex-test-full-depth):
Use nil instead of (current-time) where either will do, as nil is
a bit more efficient and should have less timing error.
2017-10-20 19:40:09 -07:00
|
|
|
(let ((t1 (make-symbol "t1")))
|
|
|
|
`(let (,t1)
|
2003-02-03 15:00:57 +00:00
|
|
|
(setq ,t1 (current-time))
|
|
|
|
,@forms
|
Prefer nil to (current-time) when either works
* doc/misc/gnus.texi (Category Syntax):
* lisp/allout-widgets.el (allout-widgets-post-command-business):
* lisp/cedet/ede/detect.el (ede-detect-qtest):
* lisp/cedet/pulse.el (pulse-momentary-highlight-overlay)
(pulse-tick):
* lisp/cedet/semantic.el (bovinate):
* lisp/cedet/semantic/analyze.el:
(semantic-analyze-current-symbol-default, semantic-adebug-analyze):
* lisp/cedet/semantic/analyze/refs.el (semantic-analyze-current-tag):
* lisp/cedet/semantic/lex.el (semantic-lex-test):
* lisp/cedet/semantic/symref/filter.el:
(semantic-symref-test-count-hits-in-tag):
* lisp/cedet/srecode/dictionary.el (srecode-adebug-dictionary):
* lisp/cedet/srecode/map.el (srecode-adebug-maps):
* lisp/desktop.el (desktop-create-buffer):
* lisp/emacs-lisp/benchmark.el (benchmark-elapse):
* lisp/emacs-lisp/elp.el (elp--make-wrapper):
* lisp/epa.el (epa--show-key):
* lisp/erc/erc.el (erc-lurker-cleanup, erc-lurker-p):
* lisp/gnus/gnus-agent.el (gnus-agent-fetch-articles)
(gnus-agent-expire-group-1, gnus-agent-store-article):
* lisp/gnus/gnus-art.el (article-lapsed-string):
* lisp/gnus/gnus-cloud.el (gnus-cloud-update-newsrc-data)
(gnus-cloud-collect-full-newsrc):
* lisp/gnus/gnus-group.el (gnus-group-timestamp-delta):
* lisp/gnus/gnus-html.el (gnus-html-cache-expired):
* lisp/gnus/gnus-score.el (gnus-score-load-file)
(gnus-decay-scores):
* lisp/gnus/nndiary.el (nndiary-expired-article-p):
* lisp/gnus/nnmail.el (nnmail-expired-article-p):
* lisp/gnus/nnmaildir.el (nnmaildir--scan):
* lisp/gnus/score-mode.el (gnus-score-edit-insert-date):
* lisp/image/gravatar.el (gravatar-cache-expired):
* lisp/net/newst-backend.el (newsticker--image-get)
(newsticker--cache-mark-expired):
* lisp/nxml/rng-maint.el (rng-time-function):
* lisp/org/org-agenda.el (org-agenda-to-appt):
* lisp/org/org-clock.el (org-clock-resolve-clock)
(org-clock-resolve, org-resolve-clocks-if-idle):
* lisp/org/org-colview.el (org-columns-edit-value, org-columns)
(org-columns-compute-all, org-agenda-columns):
* lisp/org/org-element.el (org-element--cache-interrupt-p)
(org-element--cache-sync):
* lisp/org/org-habit.el (org-habit-get-faces)
(org-habit-insert-consistency-graphs):
* lisp/org/org-indent.el (org-indent-add-properties):
* lisp/org/org-timer.el (org-timer-start)
(org-timer-pause-or-continue, org-timer-seconds)
(org-timer-show-remaining-time, org-timer-set-timer):
* lisp/org/org.el (org-babel-load-file, org-current-time)
(org-today, org-auto-repeat-maybe, org-read-date-analyze)
(org-small-year-to-year, org-goto-calendar):
* lisp/org/ox.el (org-export-insert-default-template):
* lisp/time.el (emacs-uptime):
* lisp/type-break.el (type-break-mode, type-break)
(type-break-time-warning-schedule, type-break-check):
* lisp/url/url-cache.el (url-cache-expired):
* lisp/url/url.el (url-retrieve-synchronously):
* test/lisp/char-fold-tests.el (char-fold--speed-test):
* test/manual/cedet/semantic-ia-utest.el:
(semantic-symref-test-count-hits-in-tag):
* test/manual/cedet/semantic-tests.el (semantic-idle-pnf-test)
(semantic-lex-test-full-depth):
Use nil instead of (current-time) where either will do, as nil is
a bit more efficient and should have less timing error.
2017-10-20 19:40:09 -07:00
|
|
|
(float-time (time-subtract nil ,t1)))))
|
2011-06-30 17:27:45 -07:00
|
|
|
|
2003-02-03 15:00:57 +00:00
|
|
|
;;;###autoload
|
|
|
|
(defmacro benchmark-run (&optional repetitions &rest forms)
|
|
|
|
"Time execution of FORMS.
|
|
|
|
If REPETITIONS is supplied as a number, run forms that many times,
|
|
|
|
accounting for the overhead of the resulting loop. Otherwise run
|
|
|
|
FORMS once.
|
|
|
|
Return a list of the total elapsed time for execution, the number of
|
|
|
|
garbage collections that ran, and the time taken by garbage collection.
|
|
|
|
See also `benchmark-run-compiled'."
|
2012-09-09 21:16:13 -04:00
|
|
|
(declare (indent 1) (debug t))
|
2018-03-27 16:19:40 -04:00
|
|
|
(unless (or (natnump repetitions) (and repetitions (symbolp repetitions)))
|
2003-02-03 15:00:57 +00:00
|
|
|
(setq forms (cons repetitions forms)
|
|
|
|
repetitions 1))
|
|
|
|
(let ((i (make-symbol "i"))
|
|
|
|
(gcs (make-symbol "gcs"))
|
|
|
|
(gc (make-symbol "gc")))
|
|
|
|
`(let ((,gc gc-elapsed)
|
|
|
|
(,gcs gcs-done))
|
2018-03-26 21:07:35 -04:00
|
|
|
(list ,(if (or (symbolp repetitions) (> repetitions 1))
|
2003-02-03 15:00:57 +00:00
|
|
|
;; Take account of the loop overhead.
|
|
|
|
`(- (benchmark-elapse (dotimes (,i ,repetitions)
|
|
|
|
,@forms))
|
|
|
|
(benchmark-elapse (dotimes (,i ,repetitions))))
|
|
|
|
`(benchmark-elapse ,@forms))
|
|
|
|
(- gcs-done ,gcs)
|
|
|
|
(- gc-elapsed ,gc)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defmacro benchmark-run-compiled (&optional repetitions &rest forms)
|
|
|
|
"Time execution of compiled version of FORMS.
|
|
|
|
This is like `benchmark-run', but what is timed is a funcall of the
|
|
|
|
byte code obtained by wrapping FORMS in a `lambda' and compiling the
|
|
|
|
result. The overhead of the `lambda's is accounted for."
|
2012-09-09 21:16:13 -04:00
|
|
|
(declare (indent 1) (debug t))
|
2018-03-27 16:19:40 -04:00
|
|
|
(unless (or (natnump repetitions) (and repetitions (symbolp repetitions)))
|
2003-02-03 15:00:57 +00:00
|
|
|
(setq forms (cons repetitions forms)
|
|
|
|
repetitions 1))
|
|
|
|
(let ((i (make-symbol "i"))
|
|
|
|
(gcs (make-symbol "gcs"))
|
|
|
|
(gc (make-symbol "gc"))
|
|
|
|
(code (byte-compile `(lambda () ,@forms)))
|
|
|
|
(lambda-code (byte-compile `(lambda ()))))
|
|
|
|
`(let ((,gc gc-elapsed)
|
|
|
|
(,gcs gcs-done))
|
2018-03-27 16:19:40 -04:00
|
|
|
(list ,(if (or (symbolp repetitions) (> repetitions 1))
|
2003-02-03 15:00:57 +00:00
|
|
|
;; Take account of the loop overhead.
|
|
|
|
`(- (benchmark-elapse (dotimes (,i ,repetitions)
|
|
|
|
(funcall ,code)))
|
|
|
|
(benchmark-elapse (dotimes (,i ,repetitions)
|
|
|
|
(funcall ,lambda-code))))
|
|
|
|
`(benchmark-elapse (funcall ,code)))
|
|
|
|
(- gcs-done ,gcs) (- gc-elapsed ,gc)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun benchmark (repetitions form)
|
|
|
|
"Print the time taken for REPETITIONS executions of FORM.
|
2018-02-17 11:59:36 +02:00
|
|
|
Interactively, REPETITIONS is taken from the prefix arg, and
|
|
|
|
the command prompts for the form to benchmark.
|
2009-03-17 10:32:20 +00:00
|
|
|
For non-interactive use see also `benchmark-run' and
|
2003-02-03 15:00:57 +00:00
|
|
|
`benchmark-run-compiled'."
|
|
|
|
(interactive "p\nxForm: ")
|
2018-03-26 21:07:35 -04:00
|
|
|
(let ((result (eval `(benchmark-run ,repetitions ,form) t)))
|
2003-02-03 15:00:57 +00:00
|
|
|
(if (zerop (nth 1 result))
|
|
|
|
(message "Elapsed time: %fs" (car result))
|
|
|
|
(message "Elapsed time: %fs (%fs in %d GCs)" (car result)
|
|
|
|
(nth 2 result) (nth 1 result)))))
|
|
|
|
|
|
|
|
(provide 'benchmark)
|
2003-09-01 15:45:59 +00:00
|
|
|
|
2003-02-03 15:00:57 +00:00
|
|
|
;;; benchmark.el ends here
|