Based on what Jozef said, I could write

(defn inplace-xor-hh [^bytes a ^bytes b ^bytes out]
  (hiphip.array/afill! Byte/TYPE [_ out x a y b] (bit-xor (long x) (long 
y))))

It took 2 ms on my machine, vs. 80 ms for the 'dotimes' solution.'  I think 
that matches java's speed, but if not, I'm guessing java is your only 
option.

Again, you saw how fiddly this all is: even with a library taking care of 
most of it, there was still tweaking to do (for the simplest possible 
operation).  That's why I think you should consider building on what 
Prismatic (or someone else with a byte-munging specific library, if that 
exists) has done.

--Leif

On Thursday, March 13, 2014 7:42:35 AM UTC-4, Leif wrote:
>
> Hi, Ignacio.
>
> Performance tuning in clojure being somewhat complicated, I would look for 
> prior art here.  For instance, the suggestions above give me a 6x speedup, 
> but it's still *way* slower than the equivalent java code.  So I used 
> Prismatic's hiphip (array)! library on your problem (but with longs) and 
> got close to java for-loop speed:
>
> https://github.com/Prismatic/hiphip
>
> They are focused on numerics, so they don't have support for bytes/shorts, 
> but it looks like you could add that fairly easily.  Or maybe someone has 
> already written a equivalent library for bit-twiddling.  I would look/ask 
> around.
>
> If you're interested, the main slowdown in the above suggestions is 
> probably the extra work from safe typecasts clojure does.  The hiphip 
> library uses the unsafe equivalents from clojure.lang.RT.
>
> Cheers,
> Leif
>
> On Thursday, March 13, 2014 1:26:33 AM UTC-4, Ignacio Corderi 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.

Reply via email to