Bad*_*ild 4 optimization traversal clojure sequence
;; Suppose we want to compute the min and max of a collection.
;; Ideally there would be a way to tell Clojure that we want to perform
;; only one scan, which will theoretically save a little time
;; First we define some data to test with
;; 10MM element lazy-seq
(def data (for [x (range 10000000)] (rand-int 100)))
;; Realize the lazy-seq
(dorun data)
;; Here is the amount of time it takes to go through the data once
(time (apply min data))
==> "Elapsed time: 413.805 msecs"
;; Here is the time to calc min, max by explicitly scanning twice
(time (vector (apply min data) (apply max data)))
==> "Elapsed time: 836.239 msecs"
;; Shouldn't this be more efficient since it's going over the data once?
(time (apply (juxt min max) data))
==> "Elapsed time: 833.61 msecs"
Run Code Online (Sandbox Code Playgroud)
查克,这是我使用你的解决方案后的结果:
test.core=> (def data (for [x (range 10000000)] (rand-int 100)))
#'test.core/data
test.core=> (dorun data)
nil
test.core=> (realized? data)
true
test.core=> (defn minmax1 [coll] (vector (apply min coll) (apply max coll)))
#'test.core/minmax1
test.core=> (defn minmax2 [[x & xs]] (reduce (fn [[tiny big] n] [(min tiny n) (max big n)]) [x x] xs))
#'test.core/minmax2
test.core=> (time (minmax1 data))
"Elapsed time: 806.161 msecs"
[0 99]
test.core=> (time (minmax2 data))
"Elapsed time: 6072.587 msecs"
[0 99]
Run Code Online (Sandbox Code Playgroud)
这并不能完全回答您的一般问题(即如何扫描Clojure数据结构),但值得注意的是,如果您真的关心这种代码通常会更适合专门的数据结构/库性能.
例如,使用core.matrix/vectorz-clj和一些厚颜无耻的Java互操作:
;; define the raw data
(def data (for [x (range 10000000)] (rand-int 100)))
;; convert to a Vectorz array
(def v (array :vectorz data))
(time (Vectorz/minValue v))
"Elapsed time: 18.974904 msecs"
0.0
(time (Vectorz/maxValue v))
"Elapsed time: 21.310835 msecs"
99.0
Run Code Online (Sandbox Code Playgroud)
也就是说,这比问题中给出的原始代码快20-50倍.
我怀疑你是否可以通过任何依赖扫描常规Clojure向量的代码远程接近,无论你是在一次传递还是其他方式.基本上 - 使用正确的工具来完成工作.
| 归档时间: |
|
| 查看次数: |
306 次 |
| 最近记录: |