Skip to content

Instantly share code, notes, and snippets.

@skeeto
Created December 8, 2019 15:39
Show Gist options
  • Save skeeto/36baa3b1493f53eab4e082b449448a96 to your computer and use it in GitHub Desktop.
Save skeeto/36baa3b1493f53eab4e082b449448a96 to your computer and use it in GitHub Desktop.
defalias vs. defsubst (Emacs Lisp)
;;; -*- lexical-binding: t; -*-
;; $ emacs -Q --batch -f batch-byte-compile example.el
;; $ emacs -Q --batch -l example.elc -f disas
;; $ emacs -Q --batch -l example.elc -f bench
(defun add (a b)
(+ (car a) (car b)))
(defalias 'car-alias #'car)
(defun add-alias (a b)
(+ (car-alias a) (car-alias b)))
(defsubst car-subst (x) (car x))
(defun add-subst (a b)
(+ (car-subst a) (car-subst b)))
(require 'cl-lib)
(defun add-cl-first (a b)
(+ (cl-first a) (cl-first b)))
(defun disas ()
(dolist (func '(add add-alias add-subst add-cl-first))
(disassemble func)
(princ (with-current-buffer "*Disassemble*" (buffer-string)))))
(defun bench ()
(let ((a (list 2))
(b (list 3)))
(print (benchmark-run 10000000 (add a b)))
(print (benchmark-run 10000000 (add-alias a b)))
(print (benchmark-run 10000000 (add-subst a b)))
(print (benchmark-run 10000000 (add-cl-first a b)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment