gpt4 book ai didi

optimization - 在 Clojure 中,我可以优化扫描吗?

转载 作者:行者123 更新时间:2023-12-03 16:08:59 26 4
gpt4 key购买 nike

;; 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"

Chuck,这是我使用您的解决方案后的结果:
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]

最佳答案

这并不能准确地回答您的一般问题(即如何扫描 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

即这大约是 快 20-50 倍 比问题中给出的原始代码。

我怀疑您是否会使用任何依赖于扫描常规 Clojure 向量的代码来远程接近它,无论您是一次性完成还是以其他方式完成。基本上 - 使用正确的工具来完成工作。

关于optimization - 在 Clojure 中,我可以优化扫描吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20914683/

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