Emacs Lisp Constructs Speed Comparison

By Xah Lee. Date: .

some random notes on elisp speed.

Byte Compilation

(defun silly-loop (n)
  "Return the time, in seconds, to run N iterations of a loop."
  (let ((t1 (float-time)))
    (while (> (setq n (1- n)) 0))
    (- (float-time) t1)))

(silly-loop 50000000)
;; 4.39268684387207

(byte-compile 'silly-loop)

(silly-loop 50000000)
;; 1.2310872077941895

current-word vs get-thing-at-point

(defun xah-timing (N)
  "Return the time, in seconds, to run N iterations of something."
  (let (($t1 (float-time))
        ($i 0))
    (when (< N 1) (user-error "N should be 1 or greater: %s" N))
    (while (< $i N)
      (current-word)
      (setq $i (1+ $i)))
    (- (float-time) $t1)))

(defun xah-timing2 (N)
  "Return the time, in seconds, to run N iterations of something."
  (let (($t1 (float-time))
        ($i 0))
    (when (< N 1) (user-error "N should be 1 or greater: %s" N))
    (while (< $i N)
      (thing-at-point 'word)
      (setq $i (1+ $i)))
    (- (float-time) $t1)))

(defun xah-timing3 (N)
  "Return the time, in seconds, to run N iterations of something."
  (let (($t1 (float-time))
        ($i 0))
    (when (< N 1) (user-error "N should be 1 or greater: %s" N))
    (while (< $i N)
      (xah-get-thing-at-point 'word)
      (setq $i (1+ $i)))
    (- (float-time) $t1)))

(xah-timing  40000) ; 0.09
(xah-timing2 40000) ; 0.43
(xah-timing3 40000) ; 0.10