Skip to content

Instantly share code, notes, and snippets.

@jccc
Created January 8, 2011 00:10
Show Gist options
  • Select an option

  • Save jccc/770352 to your computer and use it in GitHub Desktop.

Select an option

Save jccc/770352 to your computer and use it in GitHub Desktop.
(ns bench.ptest
(:import (java.util.concurrent Executors)))
(defn run [num-threads]
"Create a vector of atoms of size size. Create a thread-pool
of size num-threads. Each thread will iterate (1) selecting
ten random atoms from the vector and then (2) reset!ing the
lowest valued atom with the value of the highest. Each thread
has its own java.util.Random."
(let [size 1000000
atms (vec
(for [_ (range size)]
(atom (rand-int 100))))
pool (Executors/newFixedThreadPool num-threads)
tsks (map (fn [_]
(fn []
(let [rnd (java.util.Random.)]
(dotimes [_ (/ 500000 num-threads)]
(let [sel (sort-by :val
>
(map #(hash-map :val @(nth atms %)
:idx %)
(take 10 (repeatedly
#(.nextInt rnd size)))))
bst ((first sel) :val)
wst ((last sel) :idx)]
(reset! (nth atms wst) bst))))))
(range num-threads))
invk (do
(time
(doseq [futr (.invokeAll pool tsks)]
(.get futr)))
(.shutdown pool))
finl (map deref atms)]
finl))
(doseq [num-threads (range 1 10)]
(println "Number of threads: " num-threads)
(dotimes [_ 5]
(run-atoms num-threads)))
(comment
Output on a six-core machine:
Number of threads: 1
"Elapsed time: 7044.11678 msecs"
"Elapsed time: 6666.413856 msecs"
"Elapsed time: 6987.192523 msecs"
"Elapsed time: 6957.572021 msecs"
"Elapsed time: 6716.900807 msecs"
Number of threads: 2
"Elapsed time: 4096.093542 msecs"
"Elapsed time: 3587.379594 msecs"
"Elapsed time: 3829.354749 msecs"
"Elapsed time: 3547.720924 msecs"
"Elapsed time: 3796.656948 msecs"
Number of threads: 3
"Elapsed time: 2752.657352 msecs"
"Elapsed time: 2842.960399 msecs"
"Elapsed time: 2685.285828 msecs"
"Elapsed time: 3091.950402 msecs"
"Elapsed time: 2577.154845 msecs"
Number of threads: 4
"Elapsed time: 2389.983858 msecs"
"Elapsed time: 2524.445334 msecs"
"Elapsed time: 2386.567372 msecs"
"Elapsed time: 2327.005069 msecs"
"Elapsed time: 2530.353512 msecs"
Number of threads: 5
"Elapsed time: 2058.709361 msecs"
"Elapsed time: 2093.871226 msecs"
"Elapsed time: 2248.590302 msecs"
"Elapsed time: 1931.809361 msecs"
"Elapsed time: 2061.155721 msecs"
Number of threads: 6
"Elapsed time: 1973.536467 msecs"
"Elapsed time: 1722.721532 msecs"
"Elapsed time: 1811.662871 msecs"
"Elapsed time: 2064.801461 msecs"
"Elapsed time: 1820.346434 msecs"
Number of threads: 7
"Elapsed time: 1778.694158 msecs"
"Elapsed time: 2204.223439 msecs"
"Elapsed time: 1911.548074 msecs"
"Elapsed time: 1965.852013 msecs"
"Elapsed time: 1805.679752 msecs"
Number of threads: 8
"Elapsed time: 2090.42982 msecs"
"Elapsed time: 1861.543418 msecs"
"Elapsed time: 1833.07064 msecs"
"Elapsed time: 1799.785178 msecs"
"Elapsed time: 2085.152682 msecs"
Number of threads: 9
"Elapsed time: 1801.355608 msecs"
"Elapsed time: 1813.115761 msecs"
"Elapsed time: 1855.042824 msecs"
"Elapsed time: 2084.624405 msecs"
"Elapsed time: 1851.578688 msecs"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment