Skip to content

Instantly share code, notes, and snippets.

@erikkaplun
Last active August 4, 2019 00:26
Show Gist options
  • Save erikkaplun/c1ef48548d581984997849ce8dda1cc7 to your computer and use it in GitHub Desktop.
Save erikkaplun/c1ef48548d581984997849ce8dda1cc7 to your computer and use it in GitHub Desktop.
user=> (defn foo [n]
(with-local-vars [x 0]
(dotimes [i n]
(var-set x (inc @x)))))
#'user/foo
user=> (time (foo 5000000))
"Elapsed time: 514.042912 msecs"
user=> (time (foo 5000000))
"Elapsed time: 438.661454 msecs"
user=> (defn bar [n]
(let [x (atom 0)]
(dotimes [i n]
(swap! x inc))))
#'user/bar
user=> (time (bar 5000000))
"Elapsed time: 113.794149 msecs"
nil
user=> (time (bar 5000000))
"Elapsed time: 100.323271 msecs"
nil
user=> (defn baz [n]
(let [x (volatile! 0)]
(dotimes [i n]
(vswap! x inc))))
#'user/baz
user=> (time (baz 5000000))
"Elapsed time: 56.927072 msecs"
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment