Created
December 8, 2019 15:39
-
-
Save skeeto/36baa3b1493f53eab4e082b449448a96 to your computer and use it in GitHub Desktop.
defalias vs. defsubst (Emacs Lisp)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; -*- 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