Skip to content

Instantly share code, notes, and snippets.

@skeeto
Forked from Wilfred/dashtest.el
Created July 29, 2017 14:59
Show Gist options
  • Save skeeto/ef098a7d70762617620e417ce082ef7e to your computer and use it in GitHub Desktop.
Save skeeto/ef098a7d70762617620e417ce082ef7e to your computer and use it in GitHub Desktop.
dolist vs mapcar
;;; dashtest.el --- benchmarking dash -*- lexical-binding: t -*-
(defmacro --map-mapcar (form list)
(declare (debug (form form)))
`(mapcar (lambda (_) ,form) ,list))
(defmacro --map-loop (form list)
(declare (debug (form form)))
(let ((result-sym (make-symbol "result")))
`(let (,result-sym)
(dolist (_ ,list)
(push ,form ,result-sym))
(nreverse ,result-sym))))
(defun wh/benchmark ()
(interactive)
(let ((small-list (make-list 1 'foo))
(medium-list (make-list 1000 'foo))
(large-list (make-list 100000 'foo))
(it nil))
(message
"Small list with mapcar (seconds):\t%f"
(car (benchmark-run 100 (--map-mapcar it small-list))))
(message
"Small list with loop (seconds):\t\t%f"
(car (benchmark-run 100 (--map-loop it small-list))))
(message
"Medium list with mapcar (seconds):\t%f"
(car (benchmark-run 100 (--map-mapcar it medium-list))))
(message
"Medium list with loop (seconds):\t%f"
(car (benchmark-run 100 (--map-loop it medium-list))))
(message
"Large list with mapcar (seconds):\t%f"
(car (benchmark-run 100 (--map-mapcar it large-list))))
(message
"Large list with loop (seconds):\t\t%f"
(car (benchmark-run 100 (--map-loop it large-list))))))
;; Small list with mapcar (seconds): 0.000055
;; Small list with loop (seconds): 0.000018
;; Medium list with mapcar (seconds): 0.022790
;; Medium list with loop (seconds): 0.020532
;; Large list with mapcar (seconds): 1.625413
;; Large list with loop (seconds): 2.269947
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment