I think the problem is the typecasting and the bit-shifting. I think both involve casting one of the parameters to a java.lang.Number.
On Sep 7, 5:03 am, rivercheng <riverch...@gmail.com> wrote: > Hi, icemaze: > > With trying the following code, I found now Clojure is only around 15 > times slower than Java. (java6 -server) > "Elapsed time: 76.134661 msecs" > "Elapsed time: 9.509398 msecs" > > "Elapsed time: 15.137116 msecs" > "Elapsed time: 1.827812 msecs" > > "Elapsed time: 14.477701 msecs" > "Elapsed time: 1.057064 msecs" > > "Elapsed time: 15.237137 msecs" > "Elapsed time: 1.276268 msecs" > > "Elapsed time: 14.063425 msecs" > "Elapsed time: 1.050793 msecs" > (without let [words (int words)], it is 25 times slower) > "Elapsed time: 105.914041 msecs" > "Elapsed time: 8.367451 msecs" > > "Elapsed time: 52.614792 msecs" > "Elapsed time: 7.747722 msecs" > > "Elapsed time: 21.673251 msecs" > "Elapsed time: 1.074642 msecs" > > "Elapsed time: 25.225229 msecs" > "Elapsed time: 1.197158 msecs" > > "Elapsed time: 24.463993 msecs" > "Elapsed time: 1.277196 msecs" > > Most part of time is spent in the two aset clauses. > After commenting out the two asets, the speed is close to Java > version. > "Elapsed time: 13.965373 msecs" > "Elapsed time: 8.466281 msecs" > > "Elapsed time: 6.251176 msecs" > "Elapsed time: 7.716445 msecs" > > "Elapsed time: 2.434541 msecs" > "Elapsed time: 1.103253 msecs" > > "Elapsed time: 2.695829 msecs" > "Elapsed time: 1.079864 msecs" > > "Elapsed time: 2.391062 msecs" > "Elapsed time: 1.062966 msecs" > > Therefore, maybe we can check the implementation of aset to find the > reason. > Is it possible that 'aset' cannot handle the primitive type directly > and does all the boxing and un-boxing? > > BTW: I'm thinking whether it is possible to write a macro, in which > all the values with primitive types (including parameters and literals > and locally bind vars) are automatically coerced? It can make writing > time critical codes much easier. > > (import 'ArrayConverter) > (defn shorts-to-bytes [#^shorts src #^bytes dst words] > (let [words (int words)] > (loop [src-offset (int 0) > dst-offset (int 0)] > (when (< src-offset words) > (let [sample (short (aget src src-offset))] > (aset dst dst-offset (byte sample)) > (aset dst (unchecked-inc dst-offset) (byte (bit-shift- > right sample 8))) > ) > (recur (unchecked-inc src-offset) (unchecked-add 2 dst- > offset))))) > ) > > (def a (make-array Short/TYPE 512000)) > (def b (make-array Byte/TYPE 1024000)) > > (doseq [n (range 5)] > (time (shorts-to-bytes a b 512000)) > (time (ArrayConverter/shortsToBytes a b 512000)) > (println) > ) > > On Sep 7, 6:18 pm, icemaze <icem...@gmail.com> wrote: > > > @Christophe: thanks, your hint helped. > > > @B Smith-Mannschott: > > > > Questions about how best to optimize clojure code to approach java > > > performance have come up with fair regularity in the past. You might > > > find some good ideas if you search through the archives a bit. > > > I see. > > As I said before (OP), I believe that the docs, to a certain extent, > > imply that Clojure is (or should be) as performant as Java. If the > > same level of performance cannot be obtained (at least in some cases) > > it should be pointed out. > > This would set up more realistic expectations for Clojure's users and > > avoid unneeded discussion. Otherwise you end up thinking you're doing > > something wrong because your code is so slow and you write posts > > trying to understand how to write better code. > > > > This isn't always possible though, but that's where Clojure's Java > > > interop and a little pragmatism comes in: You've got there a nice > > > piece of Java code that does what you need and it's fast too. If you > > > want that speed from your Clojure code, don't waste time optimizing > > > the Clojure, just call the java implementation. > > > (I guess I'm not much of a purist.) > > > That's exactly my solution. But, again, I believe the docs should be > > corrected so that people don't waste their time trying to hopelessly > > optimize their code. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---