On Jan 28, 12:55 pm, David Nolen <[email protected]> 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 laptop, removing the "byte" hint speeds it up.
(I don't have the server version of Java.)
(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 [b (aget cpu_array i)
g (aget cpu_array (add i 1))
r (aget cpu_array (add i 2))
a (aget cpu_array (add i 3))]
(aset cpu_array i a)
(aset cpu_array (add i 1) b)
(aset cpu_array (add i 2) g)
(aset cpu_array (add i 3) r)
(recur (int (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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en