Fast vector math in Clojure / Incanter

后端 未结 5 422
感情败类
感情败类 2021-01-30 09:40

I\'m currently looking into Clojure and Incanter as an alternative to R. (Not that I dislike R, but it just interesting to try out new languages.) I like Incanter and find the s

5条回答
  •  故里飘歌
    2021-01-30 09:53

    Here's a Java arrays implementation that is on my system faster than your R code (YMMV). Note enabling the reflection warnings, which is essential when optimizing for performance, and the repeated type hint on y (the one on the def didn't seem to help for the aset) and casting everything to primitive double values (the dotimes makes sure that i is a primitive int).

    (set! *warn-on-reflection* true)
    (use 'incanter.stats)
    (def ^"[D" x (double-array (sample-normal 1e7)))
    
    (time
     (do
       (def ^"[D" y (double-array (dec (count x))))
       (dotimes [i (dec (count x))]
         (aset ^"[D" y
           i
           (double (- (double (aget x (inc i)))
                      (double (aget x i))))))))
    

提交回复
热议问题