Skip to content

Instantly share code, notes, and snippets.

@WilliamParker
Created February 10, 2016 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WilliamParker/4fc1dacaf33c4418c431 to your computer and use it in GitHub Desktop.
Save WilliamParker/4fc1dacaf33c4418c431 to your computer and use it in GitHub Desktop.
Clojure vec vs list pop performance test
;; Determine the relative performance of turning a vector into a list
;; and popping items off the list versus just using the vector.
;; The list approach is asymptotically faster, but the idea is to determine
;; at what point the overhead of creating a new list is worth it.
(defn time-list [n]
(let [init-vec (mapv (constantly 1)
(range n))]
(time (loop [l (into () init-vec)]
(let [r (rest l)
f (first r)]
(when f (recur r)))))))
(defn time-vec [n]
(let [init-vec (mapv (constantly 1)
(range n))]
(time (loop [l init-vec]
(let [r (rest l)
f (first r)]
(when f (recur r)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment