Agreed with all the comments on this so far. I would also say that dotimes 
is slower than loop for stuff like this so I would also make that change.

(defn inplace-xor [^bytes a ^bytes b ^bytes out] 
  (let [len (alength a)]
    (loop [i 0]
      (when (< i len)
        (aset-byte out i (bit-xor (aget a i) (aget b i)))
        (recur (inc i))))))

In this scenario loop/recur will use a primitive long for i.  And then I 
would look at the bytecode to verify no boxing is occurring.

On Thursday, March 13, 2014 3:36:14 AM UTC-5, Michael Gardner wrote:
>
> 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 
> <icor...@soe.ucsc.edu<javascript:>> 
> 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 clo...@googlegroups.com<javascript:> 
> > Note that posts from new members are moderated - please be patient with 
> your first post. 
> > To unsubscribe from this group, send email to 
> > clojure+u...@googlegroups.com <javascript:> 
> > 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+u...@googlegroups.com <javascript:>. 
> > 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