我只是尝试了(对我来说)一种新的编程语言:clojure。我写了一个非常天真的“筛子”实现,然后我试着优化一下。
虽然奇怪(至少对我来说),新的实施并不快,但多慢......
任何人都可以提供一些有关为什么这么慢的见解?
我也对如何改进此算法的其他技巧感兴趣...
致以最诚挚的问候,
Arnaud Gouder
; naive sieve.
(defn sieve
([max] (sieve max (range 2 max) 2))
([max candidates n]
(if (> (* n n) max)
candidates
(recur max (filter #(or (= % n) (not (= (mod % n) 0))) candidates) (inc n)))))
; Instead of just passing the 'candidates' list, from which I sieve-out the non-primes,
; I also pass a 'primes' list, with the already found primes
; I hoped that this would increase the speed, because:
; - Instead of sieving-out multiples of 'all' numbers, I now only sieve-out the multiples of primes.
; - The filter predicate now becomes simpler.
; However, this code seems to be approx 20x as slow.
; Note: the primes in 'primes' end up reversed, but I don't care (much). Adding a 'reverse' call makes it even slower :-(
(defn sieve2
([max] (sieve2 max () (range 2 max)))
([max primes candidates]
(let [n (first candidates)]
(if (> (* n n) max)
(concat primes candidates)
(recur max (conj primes n) (filter #(not (= (mod % n) 0)) (rest candidates)))))))
; Another attempt to speed things up. Instead of sieving-out multiples of all numbers in the range,
; I want to sieve-out only multiples of primes.. I don't like the '(first (filter ' construct very much...
; It doesn't seem to be faster than 'sieve'.
(defn sieve3
([max] (sieve max (range 2 max) 2))
([max candidates n]
(if (> (* n n) max)
candidates
(let [new_candidates (filter #(or (= % n) (not (= (mod % n) 0))) candidates)]
(recur max new_candidates (first (filter #(> % n) new_candidates)))))))
(time (sieve 10000000))
(time (sieve 10000000))
(time (sieve2 10000000))
(time (sieve2 10000000))
(time (sieve2 10000000))
(time (sieve 10000000)) ; Strange, speeds are very different now... Must be some memory allocation thing caused by running sieve2
(time (sieve 10000000))
(time (sieve3 10000000))
(time (sieve3 10000000))
(time (sieve 10000000))
答案 0 :(得分:4)
我有好消息和坏消息。好消息是你的直觉是正确的。
(time (sieve 10000)) ; "Elapsed time: 0.265311 msecs"
(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 ...)
(time (sieve2 10000)) ; "Elapsed time: 1.028353 msecs"
(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 ...)
坏消息是两者都比你想象的要慢得多
(time (count (sieve 10000))) ; "Elapsed time: 231.183055 msecs"
1229
(time (count (sieve2 10000))) ; "Elapsed time: 87.822796 msecs"
1229
正在发生的事情是因为过滤器是懒惰的,所以在需要打印答案之前,过滤工作才会完成。所有第一个表达式都是计数是将序列包装在一堆过滤器中的时间。将计数置于意味着序列实际上必须在时间表达式中计算,然后你会看到实际需要多长时间。
我认为在没有计数的情况下,sieve2需要更长时间,因为它在构建过滤序列时正在做一些工作。
当你计算时,sieve2更快,因为它是更好的算法。
P.S。当我尝试(时间(筛10000000))时,我的机器崩溃堆栈溢出,可能是因为它正在构建的大量嵌套过滤器调用。怎么会跑来找你?
答案 1 :(得分:2)
这种Primative数字重数学的一些优化技巧:
filter
的调用将需要装箱所有数字。 你可能在拳击/拆箱整数/整数中失去足够的时间,这将使得很难真正判断不同的优化。如果你输入提示(并使用clojure 1.3),你可能会得到更好的数字来判断你的优化。