【发布时间】:2013-11-25 07:46:08
【问题描述】:
我有一个用 Lisp(SBCL) 实现的版本,它运行时间不到 0.001 秒,有 12 个样本。然而这个版本(在 clojure 中)需要超过 1.1 秒。 我应该怎么做才能让这段代码运行得和原来的 Lisp 版本一样快?
为了确保这一点,我的数字不包括开始 repl 和其他人的时间。并且来自 sbcl 和 clojure 的时间函数。(是的,我的笔记本电脑是相当旧的基于原子的)
而且这个应用程序是/将在repl中使用,而不是在单个应用程序中编译,因此在基准测试之前运行千次似乎没有意义。
哦,fbar 是这样的:[[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 创建为不是惰性的,而是作为具体(?)向量或数组,这解决了我的问题。
【问题讨论】:
标签: performance clojure