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