Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Last active March 27, 2016 23:14
Show Gist options
  • Save timsgardner/71f0c22d656920f39b89 to your computer and use it in GitHub Desktop.
Save timsgardner/71f0c22d656920f39b89 to your computer and use it in GitHub Desktop.
(def v (volatile! nil))
;; a function that I don't think will get disappeared by the compiler:
(defn bla [x]
(vreset! v x))
(def stuff (vec (range 5e5)))
(defmacro do-reduce [[x coll] & body]
`(do
(reduce
(fn [_# ~x]
~@body
nil)
~coll)
nil))
(time
(doseq [x stuff]
(bla x)))
;; ~75 - 80 ms
(time
(do-reduce [x stuff]
(bla x)))
;; ~75 - 80 ms
(time
(dotimes [i (count stuff)]
(bla (get stuff i))))
;; ~240 - 280 ms
(time
(loop [i (int 0)]
(when (< i (count stuff))
(bla (get stuff i))
(recur (inc i)))))
;; ~280 - 300 ms
(time
(let [c (count stuff)]
(loop [i (int 0)]
(when (< i c)
(bla (get stuff i))
(recur (inc i))))))
;; ~250 - 300 ms
(time
(loop [i (int (count stuff))]
(when (not (zero? i))
(bla (get stuff i))
(recur (dec i)))))
;; ~390 - 420 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment