Created
April 3, 2014 16:54
-
-
Save wagjo/9958354 to your computer and use it in GitHub Desktop.
Upcoming reducible IO library for Clojure
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
;; Input vector of bytes | |
(def vec (into (vector-of :byte) | |
(repeatedly 1000000 #(rand-int 128)))) | |
;; Output file | |
(def out (java.io.FileOutputStream. "out.bin")) | |
;; Classic reduction -> 3835 ms | |
(let [write-fn (fn [r w] (.write out w))] | |
(time (reduce write-fn nil (map dec vec)))) | |
;; Upcoming reducible IO library -> 96 ms !!! | |
;; batch function switches reducer to batch mode | |
(let [c ^java.nio.channels.FileChannel (.getChannel out) | |
write-fn (fn [r w] (.write c ^java.nio.ByteBuffer w))] | |
(time (reduce write-fn nil (batched (map dec vec))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment