Skip to content

Instantly share code, notes, and snippets.

@neapel
Last active August 29, 2015 14:03
Show Gist options
  • Save neapel/4e502a14e3738b709672 to your computer and use it in GitHub Desktop.
Save neapel/4e502a14e3738b709672 to your computer and use it in GitHub Desktop.
Copying
(ns eval-slow
(:use [no.disassemble]
[criterium.core]
[clojure.pprint]))
; closure
(defn f1 [x] (fn [y] (= x y)))
; unquote
(defn f2 [x] (eval `(fn [y#] (= ~x y#))))
; unquote-splice
(defn f3 [x] (eval `(fn [y#] (= ~@[x] y#))))
; unquote splice closure
(defn f4 [x] (eval `(let [[x#] ~@[[x]]]
(fn [y#]
(= x# y#)))))
(def funs [["f1" f1]
["f2" f2]
["f3" f3]
["f4" f4]])
(def values [["nil" nil]
["vec" [1 2 3 4 5 6 7 8 9 10]]
["map" {1 2 3 4 5 6 7 8 9 10}]])
(print-table
(for [[fname gen-fun] funs]
(apply merge
{"name" fname}
(for [[desc value] values]
(let [out-fun (gen-fun value)
report (quick-benchmark (out-fun value) {} #_{:warmup-jit-period (* 1 s-to-ns)
:target-execution-time (* 0.01 s-to-ns)})
runtime (first (:mean report))
[scale unit] (scale-time runtime)
runtime (format "%.0f %s" (* scale runtime) unit)]
(spit (str fname "-" desc ".javap") (disassemble out-fun))
{desc runtime})))))
$ lein repl
user=> (load-file "eval_slow.clj")
| map | vec | nil | name |
|--------+--------+------+------|
| 5 ns | 5 ns | 5 ns | f1 |
| 558 ns | 134 ns | 6 ns | f2 |
| 567 ns | 130 ns | 7 ns | f3 |
| 561 ns | 117 ns | 7 ns | f4 |
(defproject eval-slow "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.6.0"]
[criterium "0.4.3"]]
:plugins [[lein-nodisassemble "0.1.3"]]
:source-paths ["./"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment