Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created November 23, 2011 15:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcromartie/1388915 to your computer and use it in GitHub Desktop.
Save jcromartie/1388915 to your computer and use it in GitHub Desktop.
fast math
(ns anyclj.math)
(defn mean
"Returns the mean value of a dataset"
[coll]
(/ (apply + coll) (count coll)))
(defn median
"Returns the median value of a dataset"
[coll]
(nth (sort coll) (/ (count coll) 2)))
(defn pow
"Returns x raised to e."
[x e]
(Math/pow x e))
(defn sqrt
"Returns square root of x."
[x]
(Math/sqrt x))
(defn variance
"Returns variance of dataset."
[coll]
(let [this-mean (mean coll)]
(mean (map #(pow (- % this-mean) 2) coll))))
(defn std-dev
"Returns the standard deviation of a dataset"
[coll]
(Math/sqrt (variance coll)))
(defmacro with-memo
"Ghetto optimization. Creates a temporary context where named fns
are bound to memoized versions, and body is evaluated with those
memoized versions."
[fns & body]
(let [bindings (mapcat #(list % `(memoize ~%)) fns)]
`(binding [~@bindings]
~@body)))
(defmacro do-fast-math
"Executes body in the context of temporary bindings where expensive
math functions are memoized. This may not actually be faster for certain
calculations, where the memoization overhead is heavier than the actual
work to be done."
[& body]
`(with-memo [mean median variance std-dev]
~@body))
(defn performance-test
[n m]
(let [nums (vec (range m))
run-test (fn [] (dotimes [i n] (std-dev nums)))]
(println "Without do-fast-math:")
(time (run-test))
(println "With do-fast-math:")
(time (do-fast-math (run-test)))))
;; observe the impact:
;;
;; anyclj.math=> (performance-test 100 1000)
;; Without do-fast-math:
;; "Elapsed time: 385.07 msecs"
;; With do-fast-math:
;; "Elapsed time: 5.484 msecs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment