On Jan 31, 11:41 am, Conrad <drc...@gmail.com> wrote:
> Hi everyone- I'm a new Clojurist trying to understand how to make use
> of Clojure parallelism... I wrote this simple program that calculates
> the number of prime numbers in a number range. It is purposely
> inefficient:
>
> (defn prime [a]
>   (not-any? #(integer? (/ a %)) (range 2 a)))
>
> (defn primes [a b]
>   (print "there are" (count (filter prime (range a b))) "primes
> between" a "and" b)
>   (prn))
>
> user=> (time (primes 1 40000))
> there are 4204 primes between 1 and 40000
> "Elapsed time: 21937.651015 msecs"
>
> Here, I calculate two such ranges in sequence with the map command:
>
> user=> (time (dorun (map #(primes %1 %2) [1 40000] [40000 60000])))
> there are  4204 primes between  1 and  40000
> there are  1854 primes between  40000 and  60000
> "Elapsed time: 48624.117451 msecs"
>
> Next, I run the same calculation with the pmap command:
>
> user=> (time (dorun (pmap #(primes %1 %2) [1 40000] [40000 60000])))
> there are  4204 primes between  1 and  40000
> there are  1854 primes between  40000 and  60000
> "Elapsed time: 49900.961506 msecs"
>
> To my befuddlement, the 'pmap version runs slower than the 'map
> version... This seems to clearly indicate that this code is not taking
> advantage of the second core of my Core2 Duo processor... Can someone
> help me understand what I'm doing wrong?
>
> Thanks in advance!

Nothing wrong on your side - pmap was not parallelizing the first num-
procs steps, since you had only 2 steps, you saw no benefit.

I've fixed pmap so it is parallel in the first n steps, SVN 1241 -
thanks for the report!

Rich

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to