Skip to content

Instantly share code, notes, and snippets.

@reborg
Created November 16, 2017 14:00
Show Gist options
  • Save reborg/8b0779c4f96049818275f5725523cdf4 to your computer and use it in GitHub Desktop.
Save reborg/8b0779c4f96049818275f5725523cdf4 to your computer and use it in GitHub Desktop.
Investigations on fast counting
(require '[criterium.core :refer [quick-bench]])
(require '[clojure.core.reducers :as r])
(import 'java.util.concurrent.atomic.AtomicInteger)
(set! *warn-on-reflection* true)
(def data
(into []
(map hash-map
(repeat :samplevalue)
(concat
(range 1e5)
(range 1e5)
(range 1e5)
(range 1e5)
(range 1e5)))))
(quick-bench
(reduce + (map (constantly 1) (filter #(= 75584 (:samplevalue %)) data))))
;; Execution time mean : 47.112245 ms
(quick-bench
(count (filter #(= 75584 (:samplevalue %)) data)))
;; Execution time mean : 32.594683 ms
(quick-bench
(count
(seq
(eduction
(map :samplevalue)
data
(filter #(= 75584 %))))))
;; Execution time mean : 25.140066 ms
(quick-bench
(transduce
(comp
(map :samplevalue)
(filter #(= 75584 %))
(map (constantly 1)))
+
data))
;; Execution time mean : 25.215213 ms
(quick-bench
(r/fold +
((comp
(map :samplevalue)
(filter #(= 75584 %))
(map (constantly 1))) +)
data))
;; Execution time mean : 13.732841 ms
(let [cnt (AtomicInteger. 0)
combinef (fn ([] cnt) ([_ _]))
rf (fn [^AtomicInteger cnt _] (.incrementAndGet cnt) cnt)
reducef ((comp (map :samplevalue) (filter #(= 75584 %))) rf)]
(quick-bench
(do
(r/fold combinef reducef data)
(long cnt))))
;; Execution time mean : 13.525181 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment