Skip to content

Instantly share code, notes, and snippets.

@pbalduino
Last active December 15, 2015 19:38
Show Gist options
  • Save pbalduino/5312577 to your computer and use it in GitHub Desktop.
Save pbalduino/5312577 to your computer and use it in GitHub Desktop.
Parallel processing in Clojure
(defn sleep []
(Thread/sleep 1000))
(defn fast-computation [x]
(* x 2))
(defn heavy-computation [x]
(sleep)
(* x 2))
; pmap helps you make heavy computations faster
(time (doall (map heavy-computation [1 2 3 4 5])))
; "Elapsed time: 5002.32348 msecs"
; (2 4 6 8 10)
(time (doall (pmap heavy-computation [1 2 3 4 5])))
; "Elapsed time: 1003.531455 msecs"
; (2 4 6 8 10)
; but your fast computations can become slower
(time (doall (map fast-computation [1 2 3 4 5])))
; "Elapsed time: 0.156445 msecs"
; (2 4 6 8 10)
(time (doall (pmap fast-computation [1 2 3 4 5])))
; "Elapsed time: 0.938946 msecs"
; (2 4 6 8 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment