Might be slow because of the polymorphic nature of nth. If you replace nth with 
aget (and turn on *warn-on-reflection*, which is a good idea when 
performance-tuning), you'll get reflection warnings because Clojure doesn't 
know what Java method to use since it doesn't know what type of objects a and b 
are. Once you silence those with type hints like in Walter's example, you get 
roughly a 2x speedup (in my tests).

BTW, I'd write it like this:

(defn inplace-xor [^bytes a ^bytes b ^bytes out]
    (dotimes [i (alength a)]
        (aset-byte out i
            (bit-xor (aget a i) (aget b i)))))

On Mar 13, 2014, at 00:26 , Ignacio Corderi <icord...@soe.ucsc.edu> wrote:

> Hey guys, here is a huge performance problem I'm trying to figure out:
> 
> ;; Say you have 2 data arrays sitting out there of 1 MB
> 
> (def one-mb (byte-array (* 1024 1024))) 
> (def another-mb (byte-array (* 1024 1024))) 
> 
> ;; and another one that should have the byte-by-byte XOR of the previous two 
> 
> (def out-mb (byte-array (* 1024 1024)))
> 
> ;; question is... how do you code this guy, so that it doesn't take forever
> 
> (defn inplace-xor [a b out]
>   (def ln (count a))
>   (loop [x 0]
>     (if (< x ln)
>       (do 
>         (aset-byte out x (bit-xor (nth a x) (nth b x)))
>         (recur (+ x 1))
>         ))))
> 
> ;; checking the time 
> 
> (time (inplace-xor one-mb another-mb out-mb))
> 
> ;; takes about ~400ms which is.... well... A LOT
> 
> ;; I'm happy to receive a solution that involves calling some java library...
> 
> Thanks in advance!
> -Ignacio
> 
>  
> 
> -- 
> 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
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to