Skip to content

Instantly share code, notes, and snippets.

@gtrak
Created July 18, 2013 17:04
Show Gist options
  • Save gtrak/6031022 to your computer and use it in GitHub Desktop.
Save gtrak/6031022 to your computer and use it in GitHub Desktop.
(def ^:dynamic *times* nil)
(defmacro timek
"Evaluates expr and prints the time it took. Returns the value of
expr."
{:added "1.0"}
[k expr]
(let [plus (fn [val val2] (if val (+ val val2) val2))]
`(let [start# (. System (nanoTime))
ret# ~expr
t# (/ (double (- (. System (nanoTime)) start#)) 1000000.0)]
(swap! *times* update-in [~k] ~plus t#)
ret#)))
(defmacro with-timek
[& body]
`(binding [*times* (atom {})]
~@body
(clojure.pprint/pprint @*times*)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment