On Tue, Dec 28, 2010 at 6:46 PM, David Nolen <dnolen.li...@gmail.com> wrote: > So it's about 5X-6X slower for 1e5. But it looks to me there's no overhead > from atom operations.
I got similar results (5x slower) on the same sorts of tests. Calling it from the class generated by genclass seemed to cause another 2x slowdown, resulting in the 10x difference I was seeing. It slows down more as you go to 10e6, which indicates that it's not just a "constant factor" slower than Java. I know that Clojure's data structures are actually log32n, but I'm so used to thinking of that as "essentially constant" that I was surprised to see such a noticeable difference in how much slower it was on 10e6 elements vs 10e5. > 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. I ran this test because I was having a discussion with someone about the benefits of Clojure's persistent data structures, and how concurrency is handled by sticking an immutable data structure into an atom, ref, or agent, rather than through "synchronized" blocks. The discussion went in the direction of memory caches as an example of the two approaches. The discussion went like this. I said, "So you get the benefit of no blocking on reading, you can even iterate over a snapshot of the hash table without blocking -- it's just a whole lot cleaner." "That must be wildly inefficient." "It's not as inefficient as you might think. [Insert discussion of log32, shared structure, etc.]" "But surely you're still paying a pretty high price. And why would you want that to be the default, paying that price all the time?" Clearly, I couldn't really answer that question without doing some investigating to see what the speed difference is like. I was even hoping that if the speed difference were small enough, I could bundle up a little library that implements HashMap as a Clojure hash map in an atom, so that my Java pals could start using Clojure's hash maps as a drop-in replacement for Java's (that's where the gen-class piece of the experiment comes in). But with a 10x speed difference and so much more memory churn, I think that's going to be a tough sell. It's possible that Clojure becomes more competitive with Java under a lot of contention for access from different threads, but I haven't tested that out yet. Anyway, thanks for confirming that the ratio of speeds on your machine are similar to mine. -- 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