Skip to content

Instantly share code, notes, and snippets.

@jfacorro
Last active July 3, 2019 06:14
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 jfacorro/35e64ecd68544ec632763e0582a90add to your computer and use it in GitHub Desktop.
Save jfacorro/35e64ecd68544ec632763e0582a90add to your computer and use it in GitHub Desktop.
Clojure persistent vector implementation benchmark comparison
(ns vector-comparison)
(defn compare-creation []
(doseq [n [0 100 1000 10000]]
(println "compare-creation with" n)
(let [coll (->erl (range n))
runs 10000]
(simple-benchmark [] (array/from_list coll) runs)
(simple-benchmark [] (clj_vector/new coll) runs))))
(defn compare-get []
(doseq [n [100 10000 1000000]]
(println "compare-get with" n)
(let [coll (->erl (range n))]
(simple-benchmark [x (array/from_list coll)] (array/get 0 x) 10000000)
(simple-benchmark [x (clj_vector/new coll)] (clj_vector/get 0 x) 10000000))))
(defn cons-array [a x] (array/set (inc (array/size a)) x a))
(defn cons-vector [v x] (clj_vector/cons x v))
(defn compare-cons []
(doseq [n [10 100 1000]]
(println ";; compare-cons with" n)
(let [coll (range n)]
(simple-benchmark [v (array/new)] (reduce cons-array v coll) 10000)
(simple-benchmark [v (clj_vector/new)] (reduce cons-vector v coll) 10000))))
(defn compare-set []
(doseq [n [33 1025 32769]]
(println ";; compare-set with" n)
(let [coll (->erl (range n))
runs 10000000]
(simple-benchmark [x (array/from_list coll)] (array/set 0 :foo x) runs)
(simple-benchmark [x (clj_vector/new coll)] (clj_vector/set 0 :foo x) runs))))
(defn pop-array [a] (array/resize (dec (array/size a)) a))
(defn pop-clj-vector [a] (clj_vector/pop a))
(defn compare-pop []
(doseq [n [10 100 1000 (+ 32768 33) (+ 32768 32) 10000]]
(println ";; compare-pop with" n)
(let [coll (->erl (range n))
runs 100000]
(simple-benchmark [x (array/from_list coll)] (pop-array x) runs)
(simple-benchmark [x (clj_vector/new coll)] (pop-clj-vector x) runs))))
(defn array-plus [_ val acc]
(apply erlang/+.2 #erl(val acc)))
(defn compare-reduce []
(doseq [n [10 100 1000]]
(println ";; compare-reduce with" n)
(let [coll (->erl (range n))
runs 10000]
(simple-benchmark [x (array/from_list coll)] (array/foldl #erl foo/array-plus.3 0 x) runs)
(simple-benchmark [x (clj_vector/new coll)] (clj_vector/reduce erlang/+.2 0 x) runs)
(simple-benchmark [x (clj_vector/new coll)] (clj_vector/reduce + 0 x) runs))))
(defn compare-to-list []
(doseq [n [100 1000 10000]]
(println ";; compare-reduce with" n)
(let [coll (->erl (range n))
runs 10000]
(simple-benchmark [x (array/from_list coll)] (array/to_list x) runs)
(simple-benchmark [x (clj_vector/new coll)] (clj_vector/to_list x) runs))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment