我在 Clojure 中找到了这段代码来筛选出前n 个素数:
(defn sieve [n]
(let [n (int n)]
"Returns a list of all primes from 2 to n"
(let [root (int (Math/round (Math/floor (Math/sqrt n))))]
(loop [i (int 3)
a (int-array n)
result (list 2)]
(if (>= i n)
(reverse result)
(recur (+ i (int 2))
(if (< i root)
(loop [arr a
inc (+ i i)
j (* i i)]
(if (>= j n)
arr
(recur (do (aset arr j (int 1)) arr)
inc
(+ j inc))))
a)
(if (zero? (aget a i))
(conj result i)
result)))))))
然后我在 Scheme 中编写了等效的(我认为)代码(我使用 mit-scheme)
(define (sieve n)
(let ((root (round (sqrt n)))
(a (make-vector n)))
(define (cross-out t to dt)
(cond ((> t to) 0)
(else
(vector-set! a t #t)
(cross-out (+ t dt) to dt)
)))
(define (iter i result)
(cond ((>= i n) (reverse result))
(else
(if (< i root)
(cross-out (* i i) (- n 1) (+ i i)))
(iter (+ i 2) (if (vector-ref a i)
result
(cons i result))))))
(iter 3 (list 2))))
计时结果是:对于 Clojure:
(time (reduce + 0 (sieve 5000000)))
"Elapsed time: 168.01169 msecs"
对于 mit 方案:
(time (fold + 0 (sieve 5000000)))
"Elapsed time: 3990 msecs"
谁能告诉我为什么 mit-scheme 慢了 20 倍以上?
更新:“不同之处在于迭代/编译模式。在我编译了 mit-scheme 代码后,它的运行速度相当快。– abo-abo 2012 年4 月 30 日 15:43 ”