Created
February 10, 2016 16:35
-
-
Save WilliamParker/4fc1dacaf33c4418c431 to your computer and use it in GitHub Desktop.
Clojure vec vs list pop performance test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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