Emacs Lisp: Benchmark, Test Speed
benchmark-run
-
(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
.
Emacs Lisp Benchmark Example: Byte Compiled Code
;; -*- coding: utf-8; lexical-binding: t; -*- ;; 2023-08-07 ;; comparing speed of compiled code (require 'benchmark) (defun xtest (n) "Return the time, in seconds, to run N iterations of a loop." (while (> (setq n (1- n)) 0)) n ) (setq xi (expt 10 7)) (benchmark-run 1 (xtest xi)) ;; (0.7 0 0.0) ;; using benchmark-run-compiled does not make a difference. strange (benchmark-run-compiled 1 (xtest xi)) ;; (0.6 0 0.0) ;; actually compile the code make a difference (byte-compile 'xtest) (benchmark-run 1 (xtest xi)) ;; (0.1 0 0.0) ;; bytecompiled code is some 7 times faster in this example
current-word vs get-thing-at-point
;; -*- coding: utf-8; lexical-binding: t; -*- ;; 2023-08-02 2023-08-07 ;; comparing speed of ways to get current word (require 'benchmark) (setq xi 100000) (benchmark-run xi (current-word)) ;; some ;; (0.19 7 0.15) (benchmark-run xi (xah-get-thing-at-point 'word)) ;; some ;; (0.55 14 0.33) ;; byte compiled version (benchmark-run xi (xah-get-thing-at-point 'word)) ;; some ;; (0.24 5 0.11) (benchmark-run xi (thing-at-point 'word)) ;; some ;; (1.1 28 0.6)
Emacs Lisp Benchmark Example: diff ways of checking the char before cursor is any of whitespace
;; -*- coding: utf-8; lexical-binding: t; -*- ;; 2023-08-02 ;; comparing speed of diff ways of checking the char before cursor is any of whitespace (require 'benchmark) (setq xi (expt 10 6)) (benchmark-run xi (progn (or (eq (char-before) 32) (eq (char-before) 9) (eq (char-before) 10)))) ;; (0.21 0 0.0) (benchmark-run xi (if (eq (point-min) (point)) nil (prog2 (backward-char) (looking-at "[ \t\n]") (forward-char)))) ;; (0.43 0 0.0) (benchmark-run xi (string-match "[ \t\n]" (char-to-string (char-before)))) ;; (0.73 20 0.47) (benchmark-run xi (looking-back "[ \t\n]")) ;; (1.5 43 1.0) (benchmark-run xi (looking-back "[ \t\n]" (1- (point)))) ;; (1.6 42 1.0)
Emacs Lisp Benchmark Example: Test a Script
(benchmark-run 1 (xahsite-update-search)) ;; (15.871795 34 0.7105949999999996) ;; (15.694725 35 0.7229780000000003)