Last active
December 15, 2015 19:38
-
-
Save pbalduino/5312577 to your computer and use it in GitHub Desktop.
Parallel processing in Clojure
This file contains hidden or 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
(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