Skip to content

Instantly share code, notes, and snippets.

@angerman
Created December 4, 2009 13:56
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 angerman/249022 to your computer and use it in GitHub Desktop.
Save angerman/249022 to your computer and use it in GitHub Desktop.
(defmacro timer
[expr]
`(let [start# (. System (nanoTime))
ret# ~expr]
(/ (double (- (. System (nanoTime)) start#)) 1000000.0)))
(defmacro statistics [n expr]
`(let [timings# (take ~n (repeatedly (fn [] (timer ~expr))))
min# (apply min timings#)
max# (apply max timings#)
mean# (/ (reduce + timings#) ~n)
median# (/ (+ min# max#) 2)
sd# (Math/sqrt (/ (reduce + (map (fn [x#] (Math/pow (- x# mean#) 2)) timings#)) (dec ~n)))]
(printf "Runs: %d
Min: %10.3f
Max: %10.3f
Mean: %10.3f
Median: %10.3f
sd: %10.3f\n"
~n min# max# mean# median# sd#)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment