On Tue, Dec 28, 2010 at 8:57 PM, Mark Engelberg <mark.engelb...@gmail.com>wrote:

> Just for fun, I was curious to see what it would be like,
> performance-wise, to simulate a synchronized mutable hash map by
> putting a clojure hash map inside an atom, and making this accessible
> to Java code via the map interface.
>

;; ~50ms-60ms
(dotimes [_ 10]
  (let [m (atom {})
        r (range 1e5)]
   (time
    (doseq [i r]
      (swap! m assoc i 2)))))

;; same as above
(dotimes [_ 10]
  (let [m {}
        r (range 1e5)]
   (time
    (loop [m m r r]
      (if (nil? r)
        m
        (recur (assoc m (first r) 2) (next r)))))))

(def ^java.util.Map j (. java.util.Collections synchronizedMap
(java.util.HashMap.)))

;; ~10ms
(dotimes [_ 10]
 (time (doseq [i (range 1e5)] (.put j i 2))))

So it's about 5X-6X slower for 1e5. But it looks to me there's no overhead
from atom operations.

I note that for 1e3 keys the difference is around 2X.

Of course this isn't much of a comparison IMO because the Java HashMap isn't
persistent. The Clojure version can add many keys as an atomic operation.
Much more useful if you're using a map as some kind of in-memory data store.

David

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