Skip to content

Instantly share code, notes, and snippets.

@jorgenschaefer
Created June 17, 2016 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgenschaefer/91c276e95229875f434ff665c93d0795 to your computer and use it in GitHub Desktop.
Save jorgenschaefer/91c276e95229875f434ff665c93d0795 to your computer and use it in GitHub Desktop.
Emacs Lisp alist vs. hash
(require 'cl)
(defun test-alis-1 (i n)
(let* ((alis (loop for i from 0 to 10 collect (list i)))
(start (float-time)))
(dotimes (_ n)
(assq i alis))
(- (float-time)
start)))
;; The above is somewhat unrealistic, though. You usually want the
;; value, not the pair, so the cdr is almost always present.
(defun test-alis-2 (i n)
(let* ((alis (loop for i from 0 to 10 collect (list i)))
(start (float-time)))
(dotimes (_ n)
(cdr (assq i alis)))
(- (float-time)
start)))
(defun test-hash (i n)
(let* ((hash (make-hash-table :test 'equal))
start)
(loop for i from 0 to 10 do (puthash i nil hash))
(setq start (float-time))
(dotimes (_ n)
(gethash i hash))
(- (float-time)
start)))
(message "alis-1: %.3f" (test-alis-1 0 100000000))
(message "alis-2: %.3f" (test-alis-2 0 100000000))
(message "hash..: %.3f" (test-hash 0 100000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment