Doing some simple microbenchmarks, I found something unexpected:

(time
 (let [v (volatile! 1)]
   (dotimes [x 100000000]
     (vreset! v @v))))
"Elapsed time: 1016.992146 msecs"

(time
 (let [v (java.util.concurrent.atomic.AtomicLong. 1)]
   (dotimes [x 100000000]
     (.set v (.get v)))))
"Elapsed time: 672.869727 msecs"

(time
 (let [v (java.util.concurrent.atomic.AtomicReference. {})]
   (dotimes [x 100000000]
     (.set v (.get v)))))
"Elapsed time: 678.143133 msecs"

(time
 (let [v (atom 1)]
   (dotimes [x 100000000]
     (reset! v @v))))
"Elapsed time: 1100.689493 msecs"

I expected volatile's to be faster. Maybe I'm not understanding the correct 
use case for volatiles but java.util.concurrent.atomic.Atomic* seems to be 
clearly the most performant. Given the downsides of using volatiles, is 
their existence justified when safer and faster alternatives exist?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to