On Fri, Jan 28, 2011 at 10:48 AM, Robert McIntyre <r...@mit.edu> wrote: > I tried to convert this java code line for line to clojure to compare > the speed differences, and boy was I surprised! > > public static void ConvertToAWT(byte[] cpuArray){ > // Given an array of bytes representing a c-style bgra image, > // converts to a java style abgr image > int len = java.lang.reflect.Array.getLength(cpuArray); > for (int i = 0; i < len; i+=4){ > byte b = cpuArray[i+0]; > byte g = cpuArray[i+1]; > byte r = cpuArray[i+2]; > byte a = cpuArray[i+3]; > cpuArray[i+0] = a; > cpuArray[i+1] = b; > cpuArray[i+2] = g; > cpuArray[i+3] = r; }} > > (defn java-like [] > (loop [i (int 0)] (if (< i buffer-size) > (let [ + clojure.core/unchecked-add > b (aget cpuArray i) > g (aget cpuArray (+ 1 i)) > r (aget cpuArray (+ 2 i)) > a (aget cpuArray (+ 3 i))] > (aset-byte cpuArray i a) > (aset-byte cpuArray (+ 1 i) b) > (aset-byte cpuArray (+ 2 i) g) > (aset-byte cpuArray (+ 3 i) r) > (recur (int (+ i 4))))))) > > (defn clojure-like [] > (doall (flatten (map (fn [[b g r a]] [a b g r]) (partition 4 4 > cpuArray))))) > > > for a byte-array of size 1920000, the java-like clojure function, a > line for line translation, takes several minutes, while the java > method takes around 3 milliseconds. > the clojure-like one takes 6 seconds. > > Why is the clojure function so much more obnoxiously slow than its > java counterpart? > Can anyone shed some light on what I'm doing wrong?
Boxing, most likely, and/or reflection. Your cpuArray needs to be produced with (int-array ...) and hinted with ^ints to get top performance. -- 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