Here's another version that's almost as fast as Rich's and it even includes
the time for computing values.

(defn mk-random-map-fast [n]
  (let [m (transient {})
        r (new ec.util.MersenneTwisterFast)
        ni (. r (nextInt))]
    (loop [i 0 result m ni ni]
      (if (= i n)
        (persistent! m)
        (recur (inc i) (assoc! m ni ni) (. r nextInt))))))

(defn fast-run [n]
  (time
   (let [m (mk-random-map-fast n)
         rs (vals m)]
     (reduce
      (fn [s k] (let [v (get m k)]
                  (+ s (if (nil? v) 0 v))))
      0
      (take n (drop (/ n 2) rs))))))

; MacBook Pro Core 2 Duo 2.53ghz JDK 1.6 64bit

; ~40ms
(dotimes [_ 10]
  (fast-run 32000))

; ~90ms
(dotimes [_ 10]
 (fast-run 64000))

; ~180ms
(dotimes [_ 10]
  (fast-run 128000))

; ~330ms
(dotimes [_ 10]
  (fast-run 256000))

; ~700ms
(dotimes [_ 10]
  (fast-run 512000))

-- 
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