David Nolen 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))))
Another slight speed-up:
(set! *warn-on-reflection* true)
(def buffer-size 1920000)
(def array (byte-array buffer-size))
(defmacro add [m n] `(unchecked-add (int ~m) (int ~n)))
(defmacro amove[a i j] `(aset ~a ~j (aget ~a ~i)))
(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)
a (aget cpu_array i4)
]
(amove cpu_array i3 i4)
(amove cpu_array i2 i3)
(amove cpu_array i i2)
(aset cpu_array i a)
(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 [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