Skip to content

Instantly share code, notes, and snippets.

@ikitommi
Last active December 28, 2018 09:55
Show Gist options
  • Save ikitommi/c1ff836389d697d08fd4e0c877d66d07 to your computer and use it in GitHub Desktop.
Save ikitommi/c1ff836389d697d08fd4e0c877d66d07 to your computer and use it in GitHub Desktop.
Elements of Clojure: positional parameters vs options vs dynamic binding
;;
;; Model Name: MacBook Pro
;; Model Identifier: MacBookPro11,3
;; Processor Name: Intel Core i7
;; Processor Speed: 2,5 GHz
;; Number of Processors: 1
;; Total Number of Cores: 4
;; L2 Cache (per Core): 256 KB
;; L3 Cache: 6 MB
;; Memory: 16 GB
;;
(require '[criterium.core :as cc])
(defn positional [x y]
(+ x y))
(defn options [opts]
(+ (:x opts) (:y opts)))
(def ^:dynamic *y* nil)
(defn dynamic [x]
(+ x *y*))
(assert (= 3
(positional 1 2)
(options {:x 1, :y 2})
(binding [*y* 2] (dynamic 1))))
(cc/quick-bench (positional 1 2)) ;; 3.7ns
(cc/quick-bench (options {:x 1, :y 2})) ;; 14.0ns
(cc/quick-bench (binding [*y* 2] (dynamic 1))) ;; 347ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment