Skip to content

Instantly share code, notes, and snippets.

@gfredericks
Created May 22, 2011 01:16
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 gfredericks/985075 to your computer and use it in GitHub Desktop.
Save gfredericks/985075 to your computer and use it in GitHub Desktop.
(ns timmc.core)
(defrecord A [x y z]
clojure.lang.IFn
(invoke [_ k]
(cond
(identical? k :x) x
(identical? k :y) y
(identical? k :z) z)))
(defn runabunch
[f]
(doseq [_ (range 10)] (time (reduce f (range 10000)))))
(defn testit
[]
(let [a (new A 18 302 3)]
(println "Calling a keyword...")
(runabunch (fn [_ _] (:y a)))
(println "Calling a record...")
(runabunch (fn [_ _] (a :y)))
(println "Calling a field...")
(runabunch (fn [_ _] (.y a)))))
; After running it a few times so it can optimize...
;
;
; user=> (testit)
; Calling a keyword...
; "Elapsed time: 3.565134 msecs"
; "Elapsed time: 3.087209 msecs"
; "Elapsed time: 3.368486 msecs"
; "Elapsed time: 2.963463 msecs"
; "Elapsed time: 2.943902 msecs"
; "Elapsed time: 3.416506 msecs"
; "Elapsed time: 2.946969 msecs"
; "Elapsed time: 3.500432 msecs"
; "Elapsed time: 7.745033 msecs"
; "Elapsed time: 5.556544 msecs"
; Calling a record...
; "Elapsed time: 5.783128 msecs"
; "Elapsed time: 5.739926 msecs"
; "Elapsed time: 9.263383 msecs"
; "Elapsed time: 3.265647 msecs"
; "Elapsed time: 3.298962 msecs"
; "Elapsed time: 2.777523 msecs"
; "Elapsed time: 3.140744 msecs"
; "Elapsed time: 2.915796 msecs"
; "Elapsed time: 2.725872 msecs"
; "Elapsed time: 2.74207 msecs"
; Calling a field...
; "Elapsed time: 2.850279 msecs"
; "Elapsed time: 2.693893 msecs"
; "Elapsed time: 2.729821 msecs"
; "Elapsed time: 3.52657 msecs"
; "Elapsed time: 2.663927 msecs"
; "Elapsed time: 2.908587 msecs"
; "Elapsed time: 3.183368 msecs"
; "Elapsed time: 3.250153 msecs"
; "Elapsed time: 2.668998 msecs"
; "Elapsed time: 2.914292 msecs"
; nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment