【发布时间】:2017-10-26 20:46:34
【问题描述】:
我是 Clojure 的新手,正在尝试使用尾递归实现指数移动平均函数。在使用lazy-seq和concat与堆栈溢出进行了一些斗争之后,我得到了以下可行的实现,但速度很慢:
(defn ema3 [c a]
(loop [ct (rest c) res [(first c)]]
(if (= (count ct) 0)
res
(recur
(rest ct)
(into;NOT LAZY-SEQ OR CONCAT
res
[(+ (* a (first ct)) (* (- 1 a) (last res)))]
)
)
)
)
)
对于 10,000 个项目的集合,Clojure 大约需要 1300 毫秒,而 Python Pandas 调用如
s.ewm(alpha=0.3, adjust=True).mean()
只需要 700 我们。我怎样才能减少这种性能差距?谢谢,
【问题讨论】: