Elisp: Benchmark: looking-back

By Xah Lee. Date: . Last updated: .

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 xmax 1000000)

(forward-char )

(benchmark-run xmax
  (progn
    (or
     (eq (char-before) 32)
     (eq (char-before) 9)
     (eq (char-before) 10))))

;; (0.21 0 0.0)

(benchmark-run xmax
  (if (eq (point-min) (point))
      nil
    (prog2
        (backward-char)
        (looking-at "[ \t\n]")
      (forward-char))))

;; (0.43 0 0.0)

(benchmark-run xmax
  (string-match "[ \t\n]" (char-to-string (char-before))))
;; (0.73 20 0.47)

(benchmark-run xmax (looking-back "[ \t\n]"))
;; (1.5 43 1.0)

(benchmark-run xmax (looking-back "[ \t\n]" (1- (point))))
;; (1.6 42 1.0)

Elisp, Benchmark and Unit Testing