loops - Clojure performance, large looping over large vectors -
i performing element-wise operations on 2 vectors on order of 50,000 elements in size, , having unsatisfactory performance issues (a few seconds). there obvious performance issues made, such using different data structure?
(defn boolean-compare "sum 1s if matching 0 otherwise" [proposal-img data-img] (sum (map #(math/abs (- (first %) (second %))) (partition 2 (interleave proposal-img data-img)))))
try this:
(apply + (map bit-xor proposal-img data-img))) some notes:
mapping function several collections uses element each arguments function - no needinterleave,partitionthis.- if data 1's , 0's,
xorfaster absolute difference
timed example:
(def data-img (repeatedly 50000 #(rand-int 2))) (def proposal-img (repeatedly 50000 #(rand-int 2))) (def sum (partial apply +)) after warming jvm...
(time (boolean-compare proposal-img data-img)) ;=> "elapsed time: 528.731093 msecs" ;=> 24802 (time (apply + (map bit-xor proposal-img data-img))) ;=> "elapsed time: 22.481255 msecs" ;=> 24802
Comments
Post a Comment