Skip to content

Instantly share code, notes, and snippets.

@jjcomer
Created January 29, 2012 12:02
Show Gist options
  • Save jjcomer/1698492 to your computer and use it in GitHub Desktop.
Save jjcomer/1698492 to your computer and use it in GitHub Desktop.
Analysis of my Median Finding Algorithms
(ns median.analyze
(:use [median core]
[incanter core charts datasets stats]))
(defn test-lists
"Generate test lists applying f to p and n"
[n p f]
(map #(rand-list (f p %)) (range n)))
(defmacro time-it
"Return the time it took to execute the expr"
[expr]
`(let [start# (. System (nanoTime))
ret# ~expr]
(/ (double (- (. System (nanoTime)) start#)) 1000000.0)))
(defn run-test
"Execute the median finding algorithms s times on input l"
[l s]
(let [rm-data (map (fn [n] [(count l) :rm n])
(take s (repeatedly (fn [] (time-it (rand-median l))))))
dm-data (map (fn [n] [(count l) :dm n])
(take s (repeatedly (fn [] (time-it (deter-median l))))))]
(concat rm-data dm-data)))
(defn build-dataset
"Create the Incanter dataset"
[n p s]
(let [test-data (test-lists n p pow)]
(dataset [:count :alg :time]
(reduce concat (map #(run-test % s) test-data)))))
(defn generate-plot
"Create a scatterplot of the algorithms' performance"
[n p s]
(with-data (build-dataset n p s)
(let [rm-data ($where {:alg {:eq :rm}})
dm-data ($where {:alg {:eq :dm}})
rm-lm (linear-model ($ :time rm-data) ($ :count rm-data))
dm-lm (linear-model ($ :time dm-data) ($ :count dm-data))]
(doto (scatter-plot ($ :count) ($ :time)
:group-by :alg
:x-label "Input Size"
:y-label "Time (ms)")
(add-lines ($ :count rm-data) (:fitted rm-lm))
(add-lines ($ :count dm-data) (:fitted dm-lm))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment