Clojure性能,大型矢量大循環 - Clojure performance, large looping over large vectors -开发者知识库

Clojure性能,大型矢量大循環 - Clojure performance, large looping over large vectors -开发者知识库,第1张

I am performing element-wise operations on two vectors on the order of 50,000 elements in size, and having unsatisfactory performance issues (a few seconds). Are there any obvious performance issues to be made, such as using a different data structure?

我在兩個向量上執行元素操作,大小為50,000個元素,並且具有不令人滿意的性能問題(幾秒鍾)。是否有任何明顯的性能問題,例如使用不同的數據結構?

(defn boolean-compare
  "Sum up 1s if matching 0 otherwise"
  [proposal-img data-img]
  (sum
  (map
    #(Math/abs (- (first %) (second %)))
    (partition 2 (interleave proposal-img data-img)))))

2 个解决方案

#1


11  

Try this:

嘗試這個:

(apply   (map bit-xor proposal-img data-img)))

Some notes:

一些說明:

  • mapping a function to several collections uses an element from each as the arguments to the function - no need to interleave and partition for this.
  • 將函數映射到多個集合使用每個集合中的元素作為函數的參數 - 不需要為此進行交錯和分區。
  • If your data is 1's and 0's, then xor will be faster than absolute difference
  • 如果您的數據是1和0,那么xor將比絕對差異更快

Timed example:

定時的例子:

(def data-img (repeatedly 50000 #(rand-int 2)))
(def proposal-img (repeatedly 50000 #(rand-int 2)))
(def sum (partial apply  ))

After warming up the JVM...

熱身后的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

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复