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 need interleave , partition this.
  • if data 1's , 0's, xor faster 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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -