gpt4 book ai didi

transactions - 为什么当中止线程计数也增加时运行时计数也会增加?

转载 作者:行者123 更新时间:2023-12-04 05:13:41 26 4
gpt4 key购买 nike

我问这个问题是为了找出我的运行时计数增加的原因。

我有一个简单的程序,它有一个数字引用向量和一堆同时尝试写入向量中的一个数字的线程。

通过修改向量的大小(在程序中称为直方图),我可以减少中止事务的数量,因为对于较大的向量大小,写集不会发生冲突。该大小在程序中称为“histsize”。

但是,当我减少中止事务的数量时,运行时间会上升!在我的系统上,当我将中止事务的数量从 2500 减少到 300 时,运行时间从 460 毫秒增加到 620 毫秒。显然,还有其他因素在起作用,但我似乎无法弄清楚它是什么。

这对我来说完全没有意义。这是代码...谁能告诉我发生了什么事?

(def histsize 5000)
(def histogram (vec (take histsize (repeatedly #(ref 0)))))
(def abort-counter (atom 0))

(defn inc_alter []
(loop [counter 10000]
(if (zero? counter)
nil
(do
(dosync
(try
(let [index (mod counter histsize)]
(ref-set (histogram index) (inc @(histogram index))))
(catch Throwable t
(do
(swap! abort-counter inc)
(throw t)))))
(recur (dec counter))))))

(defn run-histo []
(let [threads (for [x (range 0 20)] (Thread. #(inc_alter)))]
(do
(time
(do (doall (map #(.start %) threads))
(doall (map #(.join %) threads))))
(println "total aborts: " @abort-counter) )))

(run-histo)

最佳答案

也许这只是 JVM 运行时优化的问题。第一次运行可能需要更长的时间,但是您尝试过连续运行吗?我将 histogram 向量的大小从 5000 增加到 50000,连续运行 10 次得到了这个结果:

; 5000
("Elapsed time: 1221.597 msecs" total aborts: 203
"Elapsed time: 466.733 msecs" total aborts: 64
"Elapsed time: 484.87 msecs" total aborts: 127
"Elapsed time: 730.735 msecs" total aborts: 127
"Elapsed time: 461.475 msecs" total aborts: 97
"Elapsed time: 488.735 msecs" total aborts: 178
"Elapsed time: 484.342 msecs" total aborts: 42
"Elapsed time: 447.577 msecs" total aborts: 96
"Elapsed time: 478.22 msecs" total aborts: 178
"Elapsed time: 402.598 msecs" total aborts: 125
nil nil nil nil nil nil nil nil nil nil)


; 50000
("Elapsed time: 21.374 msecs" total aborts: 20
"Elapsed time: 48.55 msecs" total aborts: 20
"Elapsed time: 16.818 msecs" total aborts: 20
"Elapsed time: 13.407 msecs" total aborts: 20
"Elapsed time: 14.546 msecs" total aborts: 20
"Elapsed time: 16.687 msecs" total aborts: 20
"Elapsed time: 12.22 msecs" total aborts: 20
"Elapsed time: 13.491 msecs" total aborts: 20
"Elapsed time: 11.616 msecs" total aborts: 20
"Elapsed time: 11.896 msecs" total aborts: 20
nil nil nil nil nil nil nil nil nil nil)

修改后的run-histo:

(defn run-histo []
(for [x (range 10)]
(let [threads (for [x (range 0 20)] (Thread. #(inc_alter)))]
(do
(reset! abort-counter 0)
(time
(do (doall (map #(.start %) threads))
(doall (map #(.join %) threads))))
(println "total aborts: " @abort-counter) ))))

这在您的机器上如何运作?

关于transactions - 为什么当中止线程计数也增加时运行时计数也会增加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8138192/

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