On Jan 31, 1:40 pm, Bill James <w_a_x_...@yahoo.com> wrote:
> (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 uinc [m] `(unchecked-inc ~m))
> (defmacro amove[a i j] `(aset ~a ~j (aget ~a ~i)))
>
> (defn java-like0 [^bytes cpu_array]
>   (loop [i (int 0)]
>     (if (< i (int buffer-size))
>       (let [b (byte (aget cpu_array i))
>             g (byte (aget cpu_array (unchecked-add i (int 1))))
>             r (byte (aget cpu_array (unchecked-add i (int 2))))
>             a (byte (aget cpu_array (unchecked-add i (int 3))))]
>         (aset cpu_array i a)
>         (aset cpu_array (unchecked-add i (int 1)) b)
>         (aset cpu_array (unchecked-add i (int 2)) g)
>         (aset cpu_array (unchecked-add i (int 3)) r)
>         (recur (unchecked-add i (int 4)))))))
>
> ;;;  Make buffer-size a local.
> (defn java-like1 [^bytes cpu_array]
>   (let [buffer-size (int buffer-size)]
>     (loop [i (int 0)]
>       (if (< i (int buffer-size))
>         (let [b (byte (aget cpu_array i))
>               g (byte (aget cpu_array (unchecked-add i (int 1))))
>               r (byte (aget cpu_array (unchecked-add i (int 2))))
>               a (byte (aget cpu_array (unchecked-add i (int 3))))]
>           (aset cpu_array i a)
>           (aset cpu_array (unchecked-add i (int 1)) b)
>           (aset cpu_array (unchecked-add i (int 2)) g)
>           (aset cpu_array (unchecked-add i (int 3)) r)
>           (recur (unchecked-add i (int 4))))))))
>
> ;;;  Move byte directly from array[i] to array[j].
> ;;;  Assign index values to local variables.
> ;;;  Eliminate "byte" hint.
> (defn java-like2 [^bytes cpu_array]
>   (let [buffer-size (int buffer-size)]
>     (loop [i (int 0)]
>       (if (< i 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)))))))
>
> ;;;  Use unchecked-inc instead of unchecked-add.
> (defn java-like3 [^bytes cpu_array]
>   (let [buffer-size (int buffer-size)]
>     (loop [i (int 0)]
>       (if (< i buffer-size)
>         (let [ i2 (uinc i)
>                i3 (uinc i2)
>                i4 (uinc i3)
>                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 (uinc i4)))))))
>
> (dotimes [i 4]
>   (let [func-name (str "java-like" i)
>         func (resolve (symbol func-name))]
>     (println func-name)
>     (dotimes [_ 7]
>       (time
>         (func array)))))
>
> I think it will be faster on my laptop, but for now here are
> the results on a rather old desktop.
>
> Clojure 1.2; Java 1.5.0_07 (server); Pentium 4 3.2GHz (2 cores)
>
> java-like0
> "Elapsed time: 81.954298 msecs"
> "Elapsed time: 24.381966 msecs"
> "Elapsed time: 16.528463 msecs"
> "Elapsed time: 12.656007 msecs"
> "Elapsed time: 12.666522 msecs"
> "Elapsed time: 13.240584 msecs"
> "Elapsed time: 12.625828 msecs"
> java-like1
> "Elapsed time: 56.773426 msecs"
> "Elapsed time: 19.099019 msecs"
> "Elapsed time: 16.669669 msecs"
> "Elapsed time: 6.325035 msecs"
> "Elapsed time: 6.400674 msecs"
> "Elapsed time: 6.374463 msecs"
> "Elapsed time: 6.266154 msecs"
> java-like2
> "Elapsed time: 46.777715 msecs"
> "Elapsed time: 11.840942 msecs"
> "Elapsed time: 10.842774 msecs"
> "Elapsed time: 5.56714 msecs"
> "Elapsed time: 5.655566 msecs"
> "Elapsed time: 5.611824 msecs"
> "Elapsed time: 5.509589 msecs"
> java-like3
> "Elapsed time: 37.785107 msecs"
> "Elapsed time: 8.711968 msecs"
> "Elapsed time: 8.377959 msecs"
> "Elapsed time: 7.094104 msecs"
> "Elapsed time: 3.637105 msecs"
> "Elapsed time: 3.757499 msecs"
> "Elapsed time: 3.589582 msecs"


Much faster on my laptop.
Core2 Duo P8600 2.4GHz
Clojure 1.2
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Server VM (build 1.6.0-b105, mixed mode)

java-like0
"Elapsed time: 40.007319 msecs"
"Elapsed time: 11.8015 msecs"
"Elapsed time: 10.262477 msecs"
"Elapsed time: 10.363887 msecs"
"Elapsed time: 10.242364 msecs"
"Elapsed time: 10.163024 msecs"
"Elapsed time: 10.518935 msecs"
java-like1
"Elapsed time: 36.042849 msecs"
"Elapsed time: 8.833804 msecs"
"Elapsed time: 8.630427 msecs"
"Elapsed time: 8.084827 msecs"
"Elapsed time: 8.001296 msecs"
"Elapsed time: 8.166401 msecs"
"Elapsed time: 8.024763 msecs"
java-like2
"Elapsed time: 24.401704 msecs"
"Elapsed time: 4.934146 msecs"
"Elapsed time: 4.368991 msecs"
"Elapsed time: 4.326248 msecs"
"Elapsed time: 4.326248 msecs"
"Elapsed time: 4.328203 msecs"
"Elapsed time: 4.288813 msecs"
java-like3
"Elapsed time: 18.028548 msecs"
"Elapsed time: 1.968686 msecs"
"Elapsed time: 1.94494 msecs"
"Elapsed time: 0.991746 msecs"
"Elapsed time: 0.942299 msecs"
"Elapsed time: 0.936153 msecs"
"Elapsed time: 0.935873 msecs"


Less than 1 millisecond!
We are the winning team!

-- 
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