On Aug 10, 5:57 pm, Mark Engelberg <mark.engelb...@gmail.com> wrote: > Andy, > > My understanding is that any double that gets stored in a vector or > map is boxed, and therefore, the vast majority of your double > conversions aren't really doing anything, because when you pull them > out of the vector or map, they'll just be Double objects again. > > I believe that the biggest reason for the performance difference > between Clojure and Java on this benchmark is that if you use a > Clojure data structure (e.g., a map) to represent each "body" object, > you get a lot of boxing and unboxing of the doubles. > > So I hypothesize that the biggest bang for the buck is to find a way > in Clojure to create body objects with primitive double fields. Can > the new new do that? If not, your best bet may be to represent a body > object as a Java array of several doubles, rather than using a Clojure > map. The first double could represent the mass, the second could > represent vx, the second vy, and so on. Make macros for the accessors > that extract these values using aget. > > It's speculation on my part, but I think this will get you much closer > to Java speed than your current approach.
I've tried an approach like you suggest, using mutable Java arrays of doubles, macros using aget / aset-double for reading and writing these arrays, and loop/recur everywhere iteration is needed in the inner loop. It is here: http://github.com/jafingerhut/clojure-benchmarks/blob/173e68a52ca2e180f9d5f3ac4c81a737358d1491/n-body/nbody.clj-7.clj I was surprised to find that it is significantly slower (about 3x) than my best Clojure one so far, which uses the new transients, and vectors of doubles to store the state of the bodies -- the one I posted earlier today. http://github.com/jafingerhut/clojure-benchmarks/blob/173e68a52ca2e180f9d5f3ac4c81a737358d1491/n-body/nbody.clj-6.clj The updated summary of results is here: http://github.com/jafingerhut/clojure-benchmarks/blob/173e68a52ca2e180f9d5f3ac4c81a737358d1491/RESULTS I suspect I'm doing something wrong in my mutable Java array implementation, but I don't see what it could be. Thanks, Andy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---