Hello!

First of all, let me congratulate with the devs for their great work:
I've been using Clojure for just a couple of weeks and I had a lot of
fun learning it and experimenting with it.
I'm starting a concurrency-heavy project and I feel Clojure is gonna
be a great choice!

I'm thinking about opening a ticket but I'm not really sure. Let me
explain.
Both clojure.org and the
Pragmatic Bookshelf book seem to imply that Clojure's performance is
equivalent to Java.
If you need an example just look here:
http://clojure.org/java_interop, under "Support for Java Primitives".
Clojure's performance is great (much better than many other Lisps) but
I hit a "performance wall" in one of my functions.
This was not tragic at all: I just wrote a few lines of Java and I
built a new Java class.
It might be a problem with my code, but I read all performance-related
articles I
could find and I cannot improve things much more than this.

Basically, I want to convert a short[] to a byte[], a common need when
dealing with audio. In Clojure I wrote:

(defn shorts-to-bytes [#^shorts src #^bytes dst words]
  (loop [src-offset (int 0)
         dst-offset (int 0)]
    (when (< src-offset words)
      (let [sample (short (aget src src-offset))]
        (aset-byte dst dst-offset (byte sample))
        (aset-byte dst (inc dst-offset) (byte (bit-shift-right sample
8))))
      (recur (inc src-offset) (unchecked-add 2 dst-offset)))))


Adding type coercions helped a bit but it's still too slow. In Java
I wrote this method:

 public static void shortsToBytes(short[] src, byte[] dst, int len)
 {
   int idx = 0;
   short s;

   while (len-- > 0) {
     s = src[idx];
     dst[idx*2] = (byte)s;
     dst[idx*2+1] = (byte)(s>>>8);
     idx++;
   }
 }


Then I timed them under both Clojure 1.0 and 1.1.0-SNAPSHOT and got
similar results.

[...]
(doseq [n (range 5)]
 (time (shorts-to-bytes a b 512000))
 (time (ArrayConverter/shortsToBytes a b 512000))
 (println))


"Elapsed time: 516.527512 msecs"
"Elapsed time: 32.316904 msecs"

"Elapsed time: 472.034037 msecs"
"Elapsed time: 5.096593 msecs"

"Elapsed time: 437.755411 msecs"
"Elapsed time: 4.10872 msecs"

"Elapsed time: 535.864767 msecs"
"Elapsed time: 3.106442 msecs"

"Elapsed time: 880.127444 msecs"
"Elapsed time: 3.124339 msecs"


So Java outperforms Clojure by two orders of magnitude.

Is there anything wrong with my code? I've been fine-tuning this code
for a couple of days now, to no avail.


Thank you and keep up!
Matt.
--~--~---------~--~----~------------~-------~--~----~
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