Created
May 29, 2010 23:38
-
-
Save zakwilson/418631 to your computer and use it in GitHub Desktop.
This file contains 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
(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