I've written a small simulation program, in order to help me learn
Clojure. I've reproduced it below. It's quite likely not very
idiomatic - any suggestions on how to improve it would be nice.
However, my main concern is that it doesn't seem to scale as I would
expect when multi-threading.

The simulation is a simple test of rolling 3 6-sided dice 1,000,000
times. It uses Sean Luke's Java implementation of the Mersenne Twister
RNG. I'm using the fast (non-threadsafe) version, which is why I'm
using a separate MT instance in each thread.

The non-threaded version takes about 18 seconds to run, with CPU
showing at about 50% throughout (on my dual-core PC). The threaded
version shows CPU maxed out at 100%, but still takes 12 seconds to
run. That's not a bad speedup, but it still implies there's about a
33% overhead for multithreading.

Are these plausible sorts of figures to see? Or should I be able to
get extra performance by tricks I haven't thought of?

Thanks,
Paul.

(use 'clojure.contrib.accumulators)

(defn D [mt n m]
  (apply +
    (take n
      (repeatedly #(+ 1 (.nextInt mt m))))))

(defn histogram [s] (add-items empty-counter s))

(defn testN [count n m]
  (let [mt (ec.util.MersenneTwisterFast.)]
    (histogram (take count (repeatedly #(D mt n m))))))

(time (pr
  (testN 1000000 3 6)))
(time (pr
  (apply combine (pmap #(testN % 3 6) (repeat 100 10000)))))
(shutdown-agents)

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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