The volatile construct seems very useful in some particular cases! I have
been missing "ugly-mutable" variables for things such as certain types of
heaps/queues or write-intensive, slightly probabilistic stuff where one
missed write doesn't matter that much.

For people who don't have a Java background, I just want to point the very
useful package java.util.concurrent.atomic, in which one can find gems such
as AtomicLong, which is almost unbeatable as a counter.

An example in which an AtomicLong is about three times quicker than an
atom:

https://gist.github.com/claj/6711556#file-countertest-clj

Javadoc for AtomicLong:

http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLong.html

In the sake of completeness,
Linus

2014-09-11 11:06 GMT+02:00 Frantisek Sodomka <fsodo...@gmail.com>:

> Using my timings macro:
> https://gist.github.com/fsodomka/5890711
>
> I am getting that:
> - creation & derefing is 60% faster
> - swapping is 25% faster
> - resetting is about the same
>
>
> ;;;;;;;;;; volatile vs. atom ;;;;;;;;;;
>
> (report
>   (timings 1e7
>     (deref (volatile! 42))
>     (deref (atom 42))))
>
> ; |                  :expr |     :time | :ratio | :perc |
> ; |------------------------+-----------+--------+-------|
> ; | (deref (volatile! 42)) | 30.688238 |    1.0 | 39.81 |
> ; |      (deref (atom 42)) | 77.081141 |   2.51 | 100.0 |
>
>
> (report
>   (let [v (volatile! 42)
>         a (atom 42)]
>     (timings 1e7
>       (vswap! v inc)
>       (swap! a inc))))
>
> ; |          :expr |      :time | :ratio | :perc |
> ; |----------------+------------+--------+-------|
> ; | (vswap! v inc) | 136.052946 |    1.0 | 75.08 |
> ; |  (swap! a inc) | 181.218748 |   1.33 | 100.0 |
>
>
> (report
>   (let [v (volatile! 42)
>         a (atom 42)]
>     (timings 1e7
>       (vreset! v 10)
>       (reset! a 10))))
>
> ; |          :expr |     :time | :ratio | :perc |
> ; |----------------+-----------+--------+-------|
> ; |  (reset! a 10) | 98.755318 |    1.0 | 96.69 |
> ; | (vreset! v 10) | 102.13944 |   1.03 | 100.0 |
>
>
>
>
> On Thursday, September 11, 2014 6:18:08 AM UTC+2, puzzler wrote:
>>
>> I'm curious: how much faster are volatiles than atoms?
>>
>>  --
> 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.
>

-- 
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