Skip to content

Instantly share code, notes, and snippets.

@gtyanchev
Last active August 29, 2015 14:02
Show Gist options
  • Save gtyanchev/8599d00efe94b214295f to your computer and use it in GitHub Desktop.
Save gtyanchev/8599d00efe94b214295f to your computer and use it in GitHub Desktop.
Histogram
(ns histogram.core
(:gen-class)
(require [criterium.core :as criterium]
[clojure.pprint :as pprint]))
(defn do-sort-map [x]
(into
(sorted-map-by
(fn [key1 key2]
(compare [(get x key2) key2]
[(get x key1) key1])))
x))
(defn -main
[& arg]
(let [args (apply array-map arg)
file-path (args "-f")
number-of-threads (Integer/parseInt (args "-t"))
benchmark (Integer/parseInt (or (args "-b") "0"))
text (slurp file-path)
part-size (inc (quot (count text) number-of-threads))
parts (mapv #(apply str %) (partition-all part-size text))]
(println "File path:" file-path)
(println "Number of threads:" number-of-threads "-b" benchmark)
(println "-----------------------------------------")
(if (> benchmark 0)
(let [start (. System (nanoTime))]
(println "Starting benchmark")
(println "Running example" benchmark "times.")
(dotimes [n benchmark]
(time (apply merge-with + (mapv deref (mapv #(future (frequencies %)) parts)))))
(println "Average runtime was" (/ (double (- (. System (nanoTime)) start)) (* 1000000.0 benchmark)) "ms"))
(pprint/print-table
(map (fn [[key value]] {"character" key "frequency" value})
(do-sort-map (time (apply merge-with + (mapv deref (mapv #(future (frequencies %)) parts)))))))))
(shutdown-agents))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment