benchmark.el 4.2 KB

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