On Jan 28, 12:55 pm, David Nolen <dnolen.li...@gmail.com> wrote: > (ns atest) > > (set! *warn-on-reflection* true) > > (def buffer-size 1920000) > (def array (byte-array buffer-size)) > (defn java-like [^bytes cpuArray] > (loop [i (int 0)] > (if (< i (int buffer-size)) > (let [b (byte (aget cpuArray i)) > g (byte (aget cpuArray (unchecked-add i (int 1)))) > r (byte (aget cpuArray (unchecked-add i (int 2)))) > a (byte (aget cpuArray (unchecked-add i (int 3))))] > (aset cpuArray i a) > (aset cpuArray (unchecked-add i (int 1)) b) > (aset cpuArray (unchecked-add i (int 2)) g) > (aset cpuArray (unchecked-add i (int 3)) r) > (recur (unchecked-add i (int 4))))))) > > (dotimes [_ 10] > (time > (dotimes [_ 1] > (java-like array)))) > > On my machine this takes 9ms. > > As a comparison, the following accomplishes the same thing in 1.3.0. > > (ns test) > > (set! *unchecked-math* true) > (set! *warn-on-reflection* true) > > (def buffer-size 1920000) > (def array (byte-array buffer-size)) > (defn java-like [^bytes cpuArray] > (loop [i (int 0)] > (if (< i buffer-size) > (let [b (aget cpuArray i) > g (aget cpuArray (+ i 1)) > r (aget cpuArray (+ i 2)) > a (aget cpuArray (+ i 3))] > (aset cpuArray i a) > (aset cpuArray (+ i 1) b) > (aset cpuArray (+ i 2) g) > (aset cpuArray (+ i 3) r) > (recur (+ i 4)))))) > > (dotimes [_ 10] > (time > (dotimes [_ 1] > (java-like array))))
Faster than my previous post: (set! *warn-on-reflection* true) (def buffer-size 1920000) (def array (byte-array buffer-size)) (defmacro add [m n] (list `unchecked-add `(int ~m) `(int ~n))) (defn java-like [^bytes cpu_array] (loop [i (int 0)] (if (< i (int buffer-size)) (let [ i2 (add i 1) i3 (add i 2) i4 (add i 3) b (aget cpu_array i) g (aget cpu_array i2) r (aget cpu_array i3) a (aget cpu_array i4) ] (aset cpu_array i a) (aset cpu_array i2 b) (aset cpu_array i3 g) (aset cpu_array i4 r) (recur (add i 4)))))) (dotimes [_ 10] (time (dotimes [_ 1] (java-like array)))) -- 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