gpt4 book ai didi

clojure - LightTable 和 lein REPL 中完成时间的差异

转载 作者:行者123 更新时间:2023-12-02 04:07:58 24 4
gpt4 key购买 nike

我在 lein repl 和 LightTable Instarepl(LTIR) 之间的完成时间内遇到了奇怪的差异。例如下面的代码:

(defn lazy-primes
([] (cons 2 (lazy-seq (lazy-primes 3 [ 2 ]))))
([current calculated-primes]
(loop [ [first-prime & rest-primes] calculated-primes]
(if (> (* first-prime first-prime) current)
(cons current (lazy-seq (lazy-primes
(inc current)
(conj calculated-primes current))))
(if (= 0 (mod current first-prime))
(lazy-seq (lazy-primes (inc current) calculated-primes))
(recur rest-primes))))))

(time (last (take 10001 (lazy-primes))))

在我的 LTIR 中,它花费了:

"Elapsed time: 4535.442412 msecs"

但是在lein repl中:

"Elapsed time: 431.378074 msecs"

大约十倍的差异!

那么,问题来了——为什么差别这么大?

LTIR 和 lein repl 的 Clojure 版本为 1.7.0

此代码不是我的,它来自 codereview

最佳答案

使用这样的时间会导致非常糟糕的“微基准测试”,因为 JVM“预热”的功能可能对运行时间以及与小数据集相关的所有其他问题产生很大影响。

环境差异会通过多种方式影响该基准测试,而环境差异甚至会在很短的时间内发生变化,无法直接回答。雨果邓肯写了一条小library for accurately doing this kind of micro benchmark并且在使用它的两个平台上运行相同的代码时,您可能会得到截然不同的结果。

关于clojure - LightTable 和 lein REPL 中完成时间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34518360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com