Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Last active March 27, 2016 23:14
Show Gist options
  • Select an option

  • Save timsgardner/71f0c22d656920f39b89 to your computer and use it in GitHub Desktop.

Select an option

Save timsgardner/71f0c22d656920f39b89 to your computer and use it in GitHub Desktop.

Revisions

  1. timsgardner revised this gist Mar 27, 2016. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions looptests.clj
    Original file line number Diff line number Diff line change
    @@ -16,16 +16,16 @@
    nil))

    (time
    (doseq [i stuff]
    (bla (get stuff i))))
    (doseq [x stuff]
    (bla x)))

    ;; ~230 - 260 ms
    ;; ~75 - 80 ms

    (time
    (do-reduce [i stuff]
    (bla (get stuff i))))
    (do-reduce [x stuff]
    (bla x)))

    ;; ~220 - 260 ms
    ;; ~75 - 80 ms

    (time
    (dotimes [i (count stuff)]
  2. timsgardner revised this gist Mar 27, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion looptests.clj
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@
    (doseq [i stuff]
    (bla (get stuff i))))

    ;; ~230 ms
    ;; ~230 - 260 ms

    (time
    (do-reduce [i stuff]
  3. timsgardner created this gist Mar 27, 2016.
    60 changes: 60 additions & 0 deletions looptests.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    (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 [i stuff]
    (bla (get stuff i))))

    ;; ~230 ms

    (time
    (do-reduce [i stuff]
    (bla (get stuff i))))

    ;; ~220 - 260 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