Hi,
Here is my attempt, for the real benchmark test, it has an honorable
result of 62 sec. (if there is no flaw in my algorithm, of course).
;; file shootout/ring.clj
(ns shootout.ring
(:gen-class))
(def n-threads 503)
(def nb-pass 50000000)
(defn main
"main function calling the benchmark test."
[n-threads N print-result-fn]
(let [start-time (System/nanoTime)
agents (into [] (map #(agent {:name (inc %)}) (range n-threads)))
send-to (fn this [i token-val]
(send (nth agents (mod i n-threads))
(fn [state t-val]
(if (= t-val N)
(print-result-fn (:name state) start-time)
(this (inc i) (inc t-val)))
state)
token-val))]
(send-to 0 0)
nil))
(defn repl-print-result-fn [thread-id start-time]
(println "\n"
"thread number: " thread-id "\n"
"time (secs): " (/ (- (System/nanoTime) start-time) 1000000000.0)))
(defn benchmark-print-result-fn [thread-id _]
(println thread-id))
(defn -main []
(main n-threads nb-pass benchmark-print-result-fn))
(defn repl-test [nb-pass]
(main n-threads nb-pass repl-print-result-fn))
;; repl session
Clojure
1:1 user=> (use 'shootout.ring)
nil
1:2 user=> (do (repl-test 1000) (repl-test 50000000))
nil
1:3 user=>
thread number: 498
time (secs): 0.128037133
thread number: 292
time (secs): 62.724999733
on a recent machine
(and yes, I know something similar is on the clojure agents page, but
sometimes it's rewarding not to look at the solution too early :-)
Cheers,
--
Laurent
2009/5/29 Alvaro Vilanova Vidal <
alv...@gmail.com>: