Skip to content

Instantly share code, notes, and snippets.

@shlomiv
Last active December 28, 2015 08:19
Show Gist options
  • Save shlomiv/7470638 to your computer and use it in GitHub Desktop.
Save shlomiv/7470638 to your computer and use it in GitHub Desktop.
(require '(criterium [core :as q]))
(def ranges [-1 2 5 10 20 40 80 500 1500 Long/MAX_VALUE])
(defn create-partitioner
"returns a function that accepts a number, and returns i if the number is between h(igh) and l(ow)
otherwise returns nil"
[l h i] (fn [v] (if (and (<= v h) (> v l)) i nil) ))
(defn part
"returns a function that gets a value and returns the group which the value belongs to"
[r] (let [s (map (fn [l h i] (create-partitioner l h i)) ranges (rest ranges) (range))]
(fn [v] (some #(% v) s))))
(def p (part ranges))
(q/bench (dotimes [n 10000] (p (rand-int 1600))) :verbose) ;;; my version
(def range-groups
(into [] (map vector ranges (rest ranges))))
(defn get_group [freq]
(first
(filter
(fn[[low high]]
(and (> freq low) (<= freq high)))
range-groups)))
(q/bench (dotimes [n 10000] (get_group (rand-int 1600))) :verbose) ;;; your version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment