Skip to content

Instantly share code, notes, and snippets.

@alphapapa
Last active December 19, 2018 13:19
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 alphapapa/bef6e9bfe5d099b272d2ad6e3ebb1a63 to your computer and use it in GitHub Desktop.
Save alphapapa/bef6e9bfe5d099b272d2ad6e3ebb1a63 to your computer and use it in GitHub Desktop.
(cl-defmacro bench-lexical-binding (&key (times 1) forms ensure-equal)
"FIXME docstring"
(declare (indent defun))
`(let ((dynamic (bench-multi :times ,times :ensure-equal ,ensure-equal :raw t
:forms ,forms))
(lexical (bench-multi-lexical :times ,times :ensure-equal ,ensure-equal :raw t
:forms ,forms))
(header '("Form" "x faster than next" "Total runtime" "# of GCs" "Total GC runtime"))
combined-results)
(cl-loop for result in-ref dynamic
do (setf (car result) (format "Dynamic: %s" (car result))))
(cl-loop for result in-ref lexical
do (setf (car result) (format "Lexical: %s" (car result))))
(setq combined-results (append dynamic lexical))
(append (list header) (list 'hline) (bench-multi-process-results combined-results))))
(defun bench-multi-process-results (results)
"Return sorted RESULTS with factors added."
(setq results (sort results (-on #'< #'second)))
(cl-loop with length = (length results)
for i from 0 to (1- length)
for description = (car (nth i results))
for factor = (if (< i (1- length))
(format "%.2f" (/ (second (nth (1+ i) results))
(second (nth i results))))
"slowest")
collect (append (list description factor)
(list (format "%.6f" (second (nth i results)))
(third (nth i results))
(if (> (fourth (nth i results)) 0)
(format "%.6f" (fourth (nth i results)))
0)))))
(bench-lexical-binding :times 100 :ensure-equal t
  :forms (("buffer-local-value" (cl-loop for buffer in (buffer-list)
                                         do (buffer-local-value 'major-mode buffer)))
          ("with-current-buffer" (cl-loop for buffer in (buffer-list)
                                          do (with-current-buffer buffer
                                               major-mode)))))
Formx faster than nextTotal runtime# of GCsTotal GC runtime
Lexical: buffer-local-value1.150.00110800
Dynamic: buffer-local-value68.260.00127800
Dynamic: with-current-buffer1.010.08723600
Lexical: with-current-bufferslowest0.08816500
(bench-lexical-binding :times 100 :ensure-equal t
  :forms (("buffer-local-value" (--filter (equal 'magit-status-mode (buffer-local-value 'major-mode it))
                                          (buffer-list)))
          ("with-current-buffer" (--filter (equal 'magit-status-mode (with-current-buffer it
                                                                       major-mode))
                                           (buffer-list)))))
Formx faster than nextTotal runtime# of GCsTotal GC runtime
Lexical: buffer-local-value1.250.00126300
Dynamic: buffer-local-value55.530.00157500
Lexical: with-current-buffer1.010.08746400
Dynamic: with-current-bufferslowest0.08823000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment