Last active
December 19, 2015 15:49
-
-
Save Velrok/5978998 to your computer and use it in GitHub Desktop.
core async performance testing using a factorial function and different implementations
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
(ns async-test.core | |
(:use [clojure.core.async])) | |
(defn ! | |
([n] | |
(! 0 n)) | |
([sum n] | |
(if (zero? n) | |
sum | |
(recur (+ sum n) | |
(dec n))))) | |
(defn !range [n] | |
(for [i (range n)] | |
(! i))) | |
(defn !async-range [n] | |
(let [r-chans (for [i (range n)] | |
(go (! i)))] | |
(for [c r-chans] | |
(<!! c)))) | |
(defn !pmap-range [n] | |
(pmap #(! %) | |
(range n))) | |
;!range | |
;"Elapsed time: 2495.547 msecs" | |
;single core | |
; | |
;!async-range | |
;"Elapsed time: 1459.094 msecs" | |
;multi core | |
; | |
;!pmap-range | |
;"Elapsed time: 3165.552 msecs" | |
;multi core |
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
(ns async-test.core-spec | |
(:use [async-test.core] | |
[speclj.core])) | |
(describe "!" | |
(it "calculates the factorial" | |
(should= 0 | |
(! 0)) | |
(should= 1 | |
(! 1)) | |
(should= 3 | |
(! 2)) | |
(should= 6 | |
(! 3)) | |
(should= 55 | |
(! 10)))) | |
(describe "!range" | |
(it "runs" | |
(should (time (doall (!range 30000)))))) | |
(describe "!async-range" | |
(it "runs" | |
(should (time (doall (!async-range 30000)))))) | |
(describe "!pmap-range" | |
(it "runs" | |
(should (time (doall (!pmap-range 30000)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment