Skip to content

Instantly share code, notes, and snippets.

@jiacai2050
Created November 27, 2017 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiacai2050/7f3a08de9290d8810e8ef74e4dabb8e5 to your computer and use it in GitHub Desktop.
Save jiacai2050/7f3a08de9290d8810e8ef74e4dabb8e5 to your computer and use it in GitHub Desktop.
(def dataset (interleave (range 10000000) (range)))
(defn workflow [ds]
(->> ds
(dedupe)
(map #(* % %))
(filter #(= 0 (rem % 111)))
(take 10)))
(with-progress-reporting (bench (workflow dataset)))
;; Execution time mean : 173.265424 ns
;; Execution time std-deviation : 6.851563 ns
;; Execution time lower quantile : 164.763159 ns ( 2.5%)
;; Execution time upper quantile : 187.295385 ns (97.5%)
;; Overhead used : 1.964161 ns
(def workflow2
(comp
(dedupe)
(map #(* % %))
(filter #(= 0 (rem % 111)))
(take 10)))
(with-progress-reporting (bench (sequence workflow2 dataset)))
;; Evaluation count : 273778620 in 60 samples of 4562977 calls.
;; Execution time mean : 215.319869 ns
;; Execution time std-deviation : 3.715614 ns
;; Execution time lower quantile : 209.969636 ns ( 2.5%)
;; Execution time upper quantile : 221.657620 ns (97.5%)
;; Overhead used : 1.964161 ns
@jiacai2050
Copy link
Author

jiacai2050 commented Nov 27, 2017

;; Updated version, transducer is faster in this test.
;; It seems that only when the number of items great than a threshold, is transducer the better choice.


(def dataset (interleave (range 10000) (range)))

(defn workflow [ds]
  (->> ds
       (dedupe)
       (map #(* % %))
       (filter #(= 0 (rem % 111)))
       (reduce +)))

(with-progress-reporting (bench (workflow dataset)))

;; Evaluation count : 34260 in 60 samples of 571 calls.
;;              Execution time mean : 1.794328 ms
;;     Execution time std-deviation : 20.238946 µs
;;    Execution time lower quantile : 1.761130 ms ( 2.5%)
;;    Execution time upper quantile : 1.833225 ms (97.5%)
;;                    Overhead used : 1.964161 ns

(def workflow2
  (comp
   (dedupe)
   (map #(* % %))
   (filter #(= 0 (rem % 111)))))

(with-progress-reporting (bench (transduce workflow2 + dataset)))

;; Evaluation count : 51900 in 60 samples of 865 calls.
;;              Execution time mean : 1.143402 ms
;;     Execution time std-deviation : 24.978019 µs
;;    Execution time lower quantile : 1.117554 ms ( 2.5%)
;;    Execution time upper quantile : 1.215618 ms (97.5%)
;;                    Overhead used : 1.964161 ns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment