Created
June 17, 2016 16:38
-
-
Save jorgenschaefer/91c276e95229875f434ff665c93d0795 to your computer and use it in GitHub Desktop.
Emacs Lisp alist vs. hash
This file contains hidden or 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
(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