Skip to content

Instantly share code, notes, and snippets.

@zakwilson
Created May 29, 2010 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zakwilson/418631 to your computer and use it in GitHub Desktop.
Save zakwilson/418631 to your computer and use it in GitHub Desktop.
(use 'clojure.contrib.seq-utils)
(defn npmap [n f coll]
(apply concat
(pmap #(doall (map f %))
(partition-all (Math/ceil (/ (count coll)
(float n)))
coll))))
(defn fac
([n] (fac n 1))
([n x]
(if (= n 1)
x
(recur (- n 1) (* n x)))))
;(time (do (doall (npmap 1 fac (take 10 (repeat 50000)))) nil))
;"Elapsed time: 30053.218 msecs"
;(time (do (doall (npmap 2 fac (take 10 (repeat 50000)))) nil))
;"Elapsed time: 19077.705 msecs"
;(time (do (doall (npmap 3 fac (take 10 (repeat 50000)))) nil))
;"Elapsed time: 17598.7 msecs"
;(time (do (doall (npmap 4 fac (take 10 (repeat 50000)))) nil))
;"Elapsed time: 16788.853 msecs"
;By way of comparison, the built-ins:
;(time (do (doall (map fac (take 10 (repeat 50000)))) nil))
;"Elapsed time: 30073.203 msecs"
;(time (do (doall (pmap fac (take 10 (repeat 50000)))) nil))
;"Elapsed time: 18448.07 msecs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment