Last active
December 8, 2020 18:09
-
-
Save Hendekagon/b2f5639d6d56127dbe6b1ec83930e323 to your computer and use it in GitHub Desktop.
transducer-vs-unrolled
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
; I was curious as to whether unrolling | |
; this use of transducers would be faster | |
; | |
; in fact it's slower | |
; we're given an array which we | |
; process using fns f, g and h | |
(defn test1-xf [^bytes data] | |
(comp | |
(map (fn [i] (f data i (+ 1 i)))) | |
(partition-all 3) | |
(map (fn [p] (apply g p))) | |
(partition-all 2) | |
(map (fn [p] (apply h p))))) | |
; using transducers | |
; Execution time mean : 5.355734 µs | |
(defn test1 [^bytes data] | |
(sequence (test1-xf data) | |
(range 12 (* 12 4) 2))) | |
; unrolled | |
; Execution time mean : 8.817436 µs | |
(defmacro make-test2 [] | |
`(defn ~'test2 [~'data] | |
~(vec | |
(sequence | |
(comp | |
(map (fn [i] `(f ~'data ~i ~(+ 1 i)))) | |
(partition-all 3) | |
(map (fn [p] `(g ~@p))) | |
(partition-all 2) | |
(map (fn [[a g]] `(h ~a ~g)))) | |
(range 12 (* 12 4) 2))))) | |
(macroexpand '(make-test2)) | |
=> | |
(def test2 | |
(clojure.core/fn | |
([data] | |
[(h | |
(g | |
(f data 12 13) | |
(f data 14 15) | |
(f data 16 17)) | |
(g | |
(f data 18 19) | |
(f data 20 21) | |
(f data 22 23))) | |
(h | |
(g | |
(f data 24 25) | |
(f data 26 27) | |
(f data 28 29)) | |
(g | |
(f data 30 31) | |
(f data 32 33) | |
(f data 34 35))) | |
(h | |
(g | |
(f data 36 37) | |
(f data 38 39) | |
(f data 40 41)) | |
(g | |
(f data 42 43) | |
(f data 44 45) | |
(f data 46 47)))]))) |
Tufte profling gives the times for partition-all
and map
:
pId nCalls Min 50% ≤ 90% ≤ 95% ≤ 99% ≤ Max Mean MAD Total Clock
:map 360,000 363.00ns 2.24μs 6.16μs 6.95μs 11.76μs 4.71ms 2.99μs ±80% 1.07s 129%
:pa 320,000 116.00ns 153.00ns 5.85μs 6.27μs 10.94μs 4.71ms 1.99μs ±115% 638.06ms 76%
Accounted 1.71s 205%
Clock 834.28ms 100%
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
full benchmarks: