Skip to content

Instantly share code, notes, and snippets.

@claj
Created September 26, 2013 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claj/6711556 to your computer and use it in GitHub Desktop.
Save claj/6711556 to your computer and use it in GitHub Desktop.
Testing performance on clojure atom vs java.util.concurrent.atomic.AtomicInteger counters.
(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...
@coltnz
Copy link

coltnz commented Sep 11, 2014

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment