我有一個版本在Lisp(SBCL)中運行,它在0.001秒內運行12個樣本。然而這個版本(在clojure中)需要超過1.1秒。 我應該如何讓這段代碼運行得和原始Lisp版本一樣快?如何讓這個clojure代碼運行得更快?
爲了使它肯定的是,我的數字不包括次啓動REPL等。 (是的,我的筆記本電腦基本上是基於原子的)
而這個應用程序是/將在repl中使用,而不是在單個應用程序中編譯,所以運行千次基準測試似乎沒有意義。
哦,fbars是這樣的:[[10.0 10.5 9.8 10.1] [10.1 10.8 10.1 10.7] ...],這是 股票的開盤 - 最高 - 低 - 收盤價。
(defn- build-new-smpl [fmx fmn h l c o]
(let [fmax (max fmx h)
fmin (min fmn l)
fc (/ (+ c fmax fmin) 3.0)
fcd (Math/round (* (- fc o) 1000))
frd (Math/round (* (- (* 2.0 c) fmax fmin) 1000))]
(if (and (> fcd 0) (> frd 0))
[1 fmax fmin]
(if (and (< fcd 0) (< frd 0))
[-1 fmax fmin]
[0 fmax fmin]))))
(defn binary-smpls-using [fbars]
(let [fopen (first (first fbars))]
(loop [fbars fbars, smpls [], fmax fopen, fmin fopen]
(if (> (count fbars) 0)
(let [bar (first fbars)
[_ h l c _] bar
[nsmpl fmx fmn] (build-new-smpl fmax fmin h l c fopen)]
(recur (rest fbars) (conj smpls nsmpl) fmx fmn))
smpls))))
=========================================== =====
謝謝。我設法讓差異1000次迭代爲0.5秒(SBCL爲1.3秒,Clojure爲1.8)。主要因素是我應該創建fbars不懶惰,但作爲具體(?)矢量或數組,這解決了我的問題。