Last active
February 22, 2021 13:40
-
-
Save damesek/e7023c0bc434a42952128b2f33c702dc to your computer and use it in GitHub Desktop.
Transducers (playground)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More realistic (fixed results):