Last active
June 30, 2016 16:25
-
-
Save bilus/bb2126d560721dadbfb8 to your computer and use it in GitHub Desktop.
Channel pool vs LinkedBlockingQueue
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
(require '[clojure.core.async :as a]) | |
(defn fill-pool | |
[pool n] | |
(doseq [_ (range n)] | |
(a/>!! pool 6))) | |
(defmacro with-client-from-pool | |
[[client-binding pool] & body] | |
`(let [~client-binding (a/<!! ~pool)] | |
~@body | |
(a/>!! ~pool ~client-binding))) | |
(def my-pool (a/chan 10)) | |
(fill-pool my-pool 10) | |
(defn run | |
[x] | |
(with-client-from-pool [x my-pool] | |
(Thread/sleep 10))) | |
(time (dotimes [_ 10] | |
(doall (pmap run (range 100))))) ; => ~ 1300 msecs | |
(time (dotimes [_ 2] | |
(doall (pmap run (range 1000))))) ; => ~2650 msecs | |
(def q-pool (java.util.concurrent.LinkedBlockingQueue. 10)) | |
(dotimes [_ 10] | |
(.put q-pool _)) | |
(defmacro with-client-from-q-pool | |
[[client-binding pool] & body] | |
`(when-let [~client-binding (.poll ~pool)] | |
~@body | |
(.put ~pool ~client-binding))) | |
(defn q-run | |
[x] | |
(with-client-from-q-pool [x q-pool] | |
(Thread/sleep 10))) | |
(time (dotimes [_ 10] | |
(doall (pmap q-run (range 100))))) ; => ~ 460 msecs | |
(time (dotimes [_ 2] | |
(doall (pmap q-run (range 1000))))) ; => ~ 7400 msecs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment