Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Created December 27, 2014 21:45
Show Gist options
  • Save timsgardner/774a106aabd73d4b8520 to your computer and use it in GitHub Desktop.
Save timsgardner/774a106aabd73d4b8520 to your computer and use it in GitHub Desktop.
invocation cost
(ns invocation-cost)
(defn time-it
"Signature slightly awkward to resemble that of time-it-mac"
([f] (time-it f 1))
([n f]
(. clojure.lang.RT (StartStopwatch))
(dotimes [_ n] (f))
(let [t (. clojure.lang.RT StopStopwatch)]
(/ t n))))
(defmacro time-it-mac
([n & body]
`(do
(. clojure.lang.RT (StartStopwatch))
(dotimes [_# ~n] ~@body)
(let [t# (. clojure.lang.RT StopStopwatch)]
(/ t# ~n)))))
(def bla :bla)
(defn simpler []
bla)
(defn stress []
[(let [f simpler]
(time-it-mac 100000
(f)))
(time-it-mac 100000
bla)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment