Skip to content

Instantly share code, notes, and snippets.

@mfikes
Created April 23, 2015 17:47
Show Gist options
  • Save mfikes/7e44303e725473c30c89 to your computer and use it in GitHub Desktop.
Save mfikes/7e44303e725473c30c89 to your computer and use it in GitHub Desktop.
Compare perf of two ways to remove an element from vector by index
(defn remove-idx [coll idx] 
  (into (subvec coll 0 idx) (subvec coll (inc idx))))
(defn remove-idx' [coll idx] 
  (vec (concat (subvec coll 0 idx) (subvec coll (inc idx)))))

Remove an element from near beginning:

(defn run-bench [n]
  (let [v (vec (range n))]
    (quick-bench (remove-idx v 3))
    (quick-bench (remove-idx' v 3))))
              mean              mean
Vector size   remove-idx        remove-idx'
         10   559.869740 ns     867.888254 ns
        100    12.110264 µs       4.777121 µs
       1000   133.095287 µs      40.676972 µs
      10000     1.822973 ms     446.378637 µs

Remove an element from near end:

(defn run-bench [n]
  (let [v (vec (range n))]
    (quick-bench (remove-idx v (- n 3)))
    (quick-bench (remove-idx' v (- n 3)))))
              mean              mean
Vector size   remove-idx        remove-idx'
         10   336.695389 ns       1.309534 µs
        100   327.457512 ns      14.719970 µs
       1000   352.013322 ns     150.545199 µs
      10000   360.010662 ns       1.516899 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment