Skip to content

Instantly share code, notes, and snippets.

@damesek
Last active February 22, 2021 13:40
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 damesek/e7023c0bc434a42952128b2f33c702dc to your computer and use it in GitHub Desktop.
Save damesek/e7023c0bc434a42952128b2f33c702dc to your computer and use it in GitHub Desktop.
Transducers (playground)
(ns transducers.tests
(:require [criterium.core :refer [quick-bench]]))
(def data (into [] (concat (range 1e5)
(range 1e5)
(range 1e5)
(range 1e5)
(range 1e5))))
(defn -map [f coll]
(reduce
(fn [acc v]
(conj acc (f v)))
[]
coll))
(defn -tr-map [f coll]
(reduce
(fn [acc v]
(conj! acc (f v)))
(transient [])
coll)
persistent!)
(defn vec-trans
([] (transient []))
([acc] (persistent! acc))
([acc val] (conj! acc val)))
(defn -tr-map-xform [f]
(fn [xf]
(fn
([] (xf))
([acc] (xf acc))
([acc v]
(xf acc (f v))))))
(defn -tr-map-xform-v2 [f]
(fn [xf]
(let [prev (volatile! ::none)]
(fn
([] (xf))
([acc] (xf acc))
([acc input]
(vreset! prev input)
(xf acc (f input)))))))
(defn -mapping [f]
(fn [xf]
(fn
([] (xf))
([acc] (xf acc))
([acc v]
(xf acc (f v))))))
; doall fix via alexmiller
; http://clojure-goes-fast.com/blog/benchmarking-tool-criterium/
(quick-bench
(doall (map inc data)))
;Evaluation count : 30 in 6 samples of 5 calls.
;Execution time mean : 21,632017 ms
;Execution time std-deviation : 912,084164 µs
;Execution time lower quantile : 20,345732 ms ( 2,5%)
;Execution time upper quantile : 22,661618 ms (97,5%)
;Overhead used : 2,811131 ns
(quick-bench
(clojure.core/transduce (-tr-map-xform-v2 inc) conj data))
;Evaluation count : 30 in 6 samples of 5 calls.
;Execution time mean : 24,233903 ms
;Execution time std-deviation : 3,967989 ms
;Execution time lower quantile : 21,931465 ms ( 2,5%)
;Execution time upper quantile : 30,871784 ms (97,5%)
;Overhead used : 2,492279 ns
(quick-bench
(-map inc data))
;
;Evaluation count : 30 in 6 samples of 5 calls.
;Execution time mean : 23,948180 ms
;Execution time std-deviation : 3,320504 ms
;Execution time lower quantile : 21,632572 ms ( 2,5%)
;Execution time upper quantile : 29,581088 ms (97,5%)
;Overhead used : 2,492279 ns
(quick-bench
(-tr-map inc data))
;Evaluation count : 48 in 6 samples of 8 calls.
;Execution time mean : 11,681265 ms
;Execution time std-deviation : 281,084241 µs
;Execution time lower quantile : 11,276505 ms ( 2,5%)
;Execution time upper quantile : 11,949682 ms (97,5%)
;Overhead used : 2,492279 ns
(quick-bench
(transduce (-mapping inc) vec-trans data))
;Evaluation count : 54 in 6 samples of 9 calls.
;Execution time mean : 12,773841 ms
;Execution time std-deviation : 954,380482 µs
;Execution time lower quantile : 11,586840 ms ( 2,5%)
;Execution time upper quantile : 13,905063 ms (97,5%)
;Overhead used : 2,492279 ns
(quick-bench
(clojure.core/transduce (-tr-map-xform inc) vec-trans data))
;Evaluation count : 54 in 6 samples of 9 calls.
;Execution time mean : 12,572346 ms
;Execution time std-deviation : 472,066140 µs
;Execution time lower quantile : 12,173555 ms ( 2,5%)
;Execution time upper quantile : 13,197980 ms (97,5%)
;Overhead used : 2,492279 ns
(require '[clojure.core.reducers :as r])
(quick-bench
(r/map inc data))
;Evaluation count : 51131862 in 6 samples of 8521977 calls.
;Execution time mean : 9,847667 ns
;Execution time std-deviation : 0,224713 ns
;Execution time lower quantile : 9,591660 ns ( 2,5%)
;Execution time upper quantile : 10,080850 ns (97,5%)
;Overhead used : 2,526579 ns
@damesek
Copy link
Author

damesek commented Feb 22, 2021

More realistic (fixed results):

  (quick-bench (doall (map inc data)))
  ;Evaluation count : 24 in 6 samples of 4 calls.
  ;Execution time mean : 27,337275 ms
  ;Execution time std-deviation : 5,715984 ms
  ;Execution time lower quantile : 23,949046 ms ( 2,5%)
  ;Execution time upper quantile : 37,096827 ms (97,5%)
  ;Overhead used : 2,811131 ns
  
  (quick-bench
    (clojure.core/transduce (-tr-map-xform-v2 inc) conj data))
  ;Evaluation count : 30 in 6 samples of 5 calls.
  ;Execution time mean : 25 692277 ms
  ;Execution time std-deviation : 4,2 50145 ms
  ;Execution time lower quantile : 20,8 42035 ms ( 2,5 % )
  ;Execution time upper quantile : 29,8 39472 ms (97,5 % )
  ;Overhead used : 2,8 11131 ns
  ;=> nil

  (quick-bench
    (-map inc data))
  ;Evaluation count : 36 in 6 samples of 6 calls.
  ;Execution time mean : 24 131075 ms
  ;Execution time std-deviation : 3,2 79395 ms
  ;Execution time lower quantile : 20,7 25932 ms ( 2,5 % )
  ;Execution time upper quantile : 27,6 26224 ms (97,5 % )
  ;Overhead used : 2,8 11131 ns
  ;=> nil

  (quick-bench
    (-tr-map inc data))
  ;Evaluation count : 60 in 6 samples of 10 calls.
  ;Execution time mean : 11 845778 ms
  ;Execution time std-deviation : 638,3 98653 µs
  ;Execution time lower quantile : 11,0 80132 ms ( 2,5 % )
  ;Execution time upper quantile : 12,5 30722 ms (97,5 % )
  ;Overhead used : 2,8 11131 ns
  ;=> nil

  (quick-bench
    (transduce (-mapping inc) vec-trans data))
  ;Evaluation count : 54 in 6 samples of 9 calls.
  ;Execution time mean : 12 061591 ms
  ;Execution time std-deviation : 756,3 53887 µs
  ;Execution time lower quantile : 10,8 78726 ms ( 2,5 % )
  ;Execution time upper quantile : 12,8 58943 ms (97,5 % )
  ;Overhead used : 2,8 11131 ns
  ;=> nil

  (quick-bench
    (clojure.core/transduce (-tr-map-xform inc) vec-trans data))
  ;Evaluation count : 48 in 6 samples of 8 calls.
  ;Execution time mean : 11 874567 ms
  ;Execution time std-deviation : 898,3 93150 µs
  ;Execution time lower quantile : 11,0 53362 ms ( 2,5 % )
  ;Execution time upper quantile : 12,9 88885 ms (97,5 % )
  ;Overhead used : 2,8 11131 ns

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