Skip to content

Instantly share code, notes, and snippets.

@Velrok
Last active December 19, 2015 15:49
Show Gist options
  • Save Velrok/5978998 to your computer and use it in GitHub Desktop.
Save Velrok/5978998 to your computer and use it in GitHub Desktop.
core async performance testing using a factorial function and different implementations
(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
(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