惰性序列评估中经过的时间
Elapsed time in lazy sequence evaluation
鉴于此代码:
(reduce my-fun my-lazy-seq)
测量整个操作的运行时间:
(time (reduce my-fun my-lazy-seq)) ;;Elapsed time: 1000.1234 msecs
如何测量此循环在完成前的各个阶段所经过的时间?例如:
Elapsed time to process next 1000 samples in my-lazy-seq: 100.1234 msecs
Elapsed time to process next 1000 samples in my-lazy-seq: 99.1234 msecs
Elapsed time to process next 1000 samples in my-lazy-seq: 101.1234 msecs
...
这个怎么样:
(defn seq-counter [n coll]
(let [t0 (System/currentTimeMillis)
f (fn [i x]
(let [i (inc i)]
(if (= 0 (rem i n))
(println i "items processed in" (- (System/currentTimeMillis) t0) "ms."))
x))]
(map-indexed f coll)))
map-indexed
用于查看进度。上面的函数将在每个 n
个元素中打印计数和处理时间。
user=> (reduce + (seq-counter 10 (range 100)))
10 items processed in 0 ms.
20 items processed in 0 ms.
...
100 items processed in 1 ms.
4950
参考Idiomatic clojure for progress reporting?
(doseq [thousand (partition 1000 my-lazy-seq)]
(time (reduce my-fun thousand)))
鉴于此代码:
(reduce my-fun my-lazy-seq)
测量整个操作的运行时间:
(time (reduce my-fun my-lazy-seq)) ;;Elapsed time: 1000.1234 msecs
如何测量此循环在完成前的各个阶段所经过的时间?例如:
Elapsed time to process next 1000 samples in my-lazy-seq: 100.1234 msecs
Elapsed time to process next 1000 samples in my-lazy-seq: 99.1234 msecs
Elapsed time to process next 1000 samples in my-lazy-seq: 101.1234 msecs
...
这个怎么样:
(defn seq-counter [n coll]
(let [t0 (System/currentTimeMillis)
f (fn [i x]
(let [i (inc i)]
(if (= 0 (rem i n))
(println i "items processed in" (- (System/currentTimeMillis) t0) "ms."))
x))]
(map-indexed f coll)))
map-indexed
用于查看进度。上面的函数将在每个 n
个元素中打印计数和处理时间。
user=> (reduce + (seq-counter 10 (range 100)))
10 items processed in 0 ms.
20 items processed in 0 ms.
...
100 items processed in 1 ms.
4950
参考Idiomatic clojure for progress reporting?
(doseq [thousand (partition 1000 my-lazy-seq)]
(time (reduce my-fun thousand)))