我有一个版本在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不懒惰,但作为具体(?)矢量或数组,这解决了我的问题。