Skip to content

Instantly share code, notes, and snippets.

@nasser
Forked from timsgardner/looptests.clj
Last active March 27, 2016 23:15
Show Gist options
  • Save nasser/9cd5c86a03f0b6adfc60 to your computer and use it in GitHub Desktop.
Save nasser/9cd5c86a03f0b6adfc60 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)))
;; ~15 - 21 ms
(time
(do-reduce [x stuff]
(bla x)))
;; ~13 - 24 ms
(time
(dotimes [i (count stuff)]
(bla (get stuff i))))
;; ~58 - 63 ms
(time
(loop [i (int 0)]
(when (< i (count stuff))
(bla (get stuff i))
(recur (inc i)))))
;; ~65 - 68 ms
(time
(let [c (count stuff)]
(loop [i (int 0)]
(when (< i c)
(bla (get stuff i))
(recur (inc i))))))
;; ~59 - 66 ms
(time
(loop [i (int (count stuff))]
(when (not (zero? i))
(bla (get stuff i))
(recur (dec i)))))
;; ~75 - 82 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment