为什么Clojure Reducers r / fold不提供perf perf? [英] Why does this Clojure Reducers r/fold provide no perf benefit?
问题描述
我想知道为什么下面的代码提供没有加速的情况下r / fold?我是否对reducer有所了解?
I'm wondering why the code below provides no speedup in the case of r/fold? Am I misunderstanding something about reducers?
我使用Ubuntu 12.04开发盒运行速度非常慢(虽然有两个核心),通过emacs和lein run,具有相同的结果。
I'm running it on a pretty slow (although with 2 cores) Ubuntu 12.04 dev box, both through emacs and lein run, each with the same results.
(require '[clojure.core.reducers :as r])
(.. Runtime getRuntime availableProcessors)
;; 2
(let
[n 80000000
vs #(range n)]
(time (reduce + (vs)))
(time (r/fold + (vs)))
已用时间:26076.434324毫秒
已用时间:25500.234034 msecs
"Elapsed time: 26076.434324 msecs"
"Elapsed time: 25500.234034 msecs"
>
推荐答案
你折叠一个seq。平行折叠只发生在持久性向量和地图上。
You are folding over a seq. Parallel fold only happens on persistent vectors and maps right now.
也有各种各样的原因,为什么这种perf测试不如使用像 Criterium ,但是这可能是一个单独的讨论(一些原因是垃圾回收,JVM预热和内联,在Emacs和lein,boxed和checked数学等上的有趣的默认jvm设置)。
There are also all sorts of reasons why this kind of perf testing is inferior to using something like Criterium, but that's probably a separate discussion. (Some of the reasons are garbage collection, JVM warmup and inlining, funky default jvm settings on both Emacs and lein, boxed and checked math, etc.)
对于上面的许多原因仍然错误,但稍微更有用的比较:
Still wrong for many of the above reasons but slightly more useful comparison:
(require '[clojure.core.reducers :as r])
(def v (vec (range 800000)))
(dotimes [_ 100] (time (reduce + v)))
(dotimes [_ 100] (time (r/fold + v)))
的最后2次跑步。
这篇关于为什么Clojure Reducers r / fold不提供perf perf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!