One thing you need to do is define what you mean exactly when you say "Java vs Clojure." In your example you are comparing clojure code vs java code but you are also comparing clojure data structures (PersistentMap) with traditional Java data structures (HashMap). I'm not sure you meant to conflate the two.
Also, there have been plenty of threads that start off comparing java and clojure performance and eventually end up at near-equivalent performance after using type hints/coercion/etc. It has become something of a FAQ. I'd start with: http://groups.google.com/groups/search?q=group:clojure+performance&qt_s=Search+Groups <http://groups.google.com/groups/search?q=group:clojure+performance&qt_s=Search+Groups> -- Aaron On Mon, Jul 27, 2009 at 1:06 PM, BerlinBrown <berlin.br...@gmail.com> wrote: > > I was coming up with some performance tests for Clojure, going back > and forth between different JVM languages (JRuby, Scala) and mainly > looking at pure Java code. So far I have found that clojure is about > 5-10 times as slow as comparable code in Clojure. Of course this is > before any optimizations. > > Here is my question, do you have any code both Clojure and Java where > there is a one to one match between code where Clojure runs as fast as > Java. It could be anything. > > Here is one example: > This is code from clojure/contrib to convert a list of data into a > frequency count map. Pretty standard stuff. > > (defn frequencies > "Returns a map from distinct items in coll to the number of times > they appear." > [coll] > ;;;;;;;;; > (reduce (fn [counts x] > (assoc counts x (inc (get counts x 0)))) > {} coll)) > > (dotimes [x 4] > (println "i: " (int (Math/pow 10.0 x))) > (time > (dotimes [_ (int (Math/pow 10.0 x))] > (let [a (for [_ (range 1000)] (random-string 3))] > (frequencies a))))) > > ---------------------- > > public static Map frequencies(final List list) { > final Map map = new HashMap(); > // Simple Box and then unbox the count as the value for this > map // > for (Iterator it = list.iterator(); it.hasNext(); ) { > final Object o = it.next(); > final String s = o.toString(); > Integer prev = (Integer) map.get(s); > if (prev == null) { > // Put a new value on th map > final Integer count = ONE; > map.put(s, count); > } else { > final Integer inc = new Integer(prev.intValue() + 1); > map.put(s, inc); > } // End of the if - else // > } > return map; > } > ---------------------- > > Clojure Results (same machine 1.5 JDK) > > i: 1 > "Elapsed time: 51.657859 msecs" > i: 10 > "Elapsed time: 212.568221 msecs" > i: 100 > "Elapsed time: 1623.107702 msecs" > i: 1000 > "Elapsed time: 16356.185166 msecs" > (used:2M/0M [2M,63M ]) > Done > Performing simple garbage collection cooldown > (used:2M/0M [2M,63M ]) > (used:2M/0M [2M,63M ]) > > ------------- > > Here are the Java results. > > 14.631606999999999 ms > 4.828342999999999 ms > i: 10 > Elapsed time: 9.803628 msecs > i: 100 > Elapsed time: 97.562451 msecs > i: 1000 > Elapsed time: 972.775771 msecs > (used:1M/0M [1M,63M ]) > (used:1M/0M [1M,63M ]) > (used:1M/0M [1M,63M ]) > > NOTE:!!! This is just one example. All my other examples, ended up > the same way. I hope you don't nitpick this one. > > I know we should take performance tests with a grain a salt. But at > the same time, I feel we should at least try to measure the speed of > some of our code. I haven't found many speed tests out there on > clojure. > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---