Created
September 26, 2013 08:47
-
-
Save claj/6711556 to your computer and use it in GitHub Desktop.
Testing performance on clojure atom vs java.util.concurrent.atomic.AtomicInteger counters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns countertest) | |
(set *warn-on-reflection* true) | |
(def atom-cnt (atom 0)) | |
(time (dotimes [_ 1e6] (swap! atom-cnt inc))) | |
;; 156 ms | |
(def ^java.util.concurrent.atomic.AtomicInteger atomic-integer-counter (java.util.concurrent.atomic.AtomicInteger. 0)) | |
(time (dotimes [_ 1e6] (.incrementAndGet atomic-integer-counter))) | |
;; 43 ms | |
;;when atomic-integer-counter not correctly typed, the reflections make the result 4500 ms = 100 times worse... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The VM must warm up.
user=> (dotimes [_ 3](time %28dotimes [_ 1e6] %28swap! atom-cnt inc%29%29))
"Elapsed time: 37.268 msecs"
"Elapsed time: 14.288 msecs"
"Elapsed time: 14.278 msecs"
nil
user=> (def ^java.util.concurrent.atomic.AtomicInteger atomic-integer-counter (java.util.concurrent.atomic.AtomicInteger. 0))
'user/atomic-integer-counter
user=> (dotimes [_ 3](time %28dotimes [_ 1e6] %28.incrementAndGet atomic-integer-counter%29%29))
"Elapsed time: 15.439 msecs"
"Elapsed time: 9.795 msecs"
"Elapsed time: 9.88 msecs"