Skip to content

Instantly share code, notes, and snippets.

@wagjo
Created April 3, 2014 16:54
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 wagjo/9958354 to your computer and use it in GitHub Desktop.
Save wagjo/9958354 to your computer and use it in GitHub Desktop.
Upcoming reducible IO library for Clojure
;; 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