Skip to content

Instantly share code, notes, and snippets.

@tnoda
Forked from halcat0x15a/big.clj
Created September 5, 2012 05:53
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 tnoda/3631266 to your computer and use it in GitHub Desktop.
Save tnoda/3631266 to your computer and use it in GitHub Desktop.
user> (bench insert-vector [] 1000)
"Elapsed time: 117.616 msecs"
[835 483 229 138 454 643 450 636 792 357 35 455 986 249 181 25 966 519 590 71 ...]
user> (bench insert-vector' [] 1000)
"Elapsed time: 61.382 msecs"
[545 468 921 491 728 783 577 15 620 231 787 537 8 435 730 842 334 55 527 534 ...]
bench-insert=> (bench insert-zipper (zip/vector-zip []) 200)
"Elapsed time: 17.650291 msecs"
[[69 166 146 84 59 77 130 52 175 4 125 126 179 172 188 181 193 76 74 34 64 177 66 30 173 184 121 51 60 178 11 156 134 194 17 102 62 185 5 43 71 83 87 138 196 167 38 169 187 137 106 115 1 162 101 180 79 131 109 105 42 136 65 95 75 57 55 108 8 160 191 45 157 133 114 53 155 153 103 54 111 41 171 176 2 40 15 118 31 96 28 145 22 189 37 48 151 143 129 78 128 14 82 63 140 26 81 89 91 86 161 10 124 92 117 127 58 186 190 32 88 12 33 144 132 36 163 198 119 16 158 85 195 116 98 90 148 23 47 3 123 170 113 174 80 46 183 72 122 9 7 147 56 100 24 73 25 150 142 152 99 120 21 67 97 139 68 149 110 159 197 165 192 94 164 61 93 20 6 141 135 44 13 199 29 50 112 35 182 107 27 168 19 49 104 39 18 154 70 0] {:changed? true}]
bench-insert=> (bench insert-vector [] 200)
"Elapsed time: 10.157477 msecs"
[90 154 123 110 174 83 3 63 104 94 60 50 105 191 19 40 86 84 46 57 162 28 176 146 65 153 199 67 30 10 61 102 8 196 20 38 138 21 165 155 23 168 136 43 99 96 116 18 69 39 68 109 58 173 198 139 26 62 14 52 29 106 134 97 1 98 135 131 147 78 42 113 9 144 151 111 16 148 17 150 36 72 184 169 142 130 167 107 170 117 152 95 76 179 89 49 81 112 54 15 163 73 75 25 37 100 194 32 85 120 13 180 166 181 114 115 119 143 164 183 137 161 74 182 108 93 64 55 185 190 7 48 31 145 175 195 33 12 88 92 118 188 22 158 87 187 45 129 2 186 197 71 177 122 56 140 156 66 172 34 59 159 160 24 189 157 41 133 178 47 44 82 126 171 77 124 35 4 91 51 193 132 53 149 5 79 192 128 11 70 6 125 101 141 103 80 121 27 127 0]
bench-insert=>
ClojureScript:bench-insert> (bench insert-zipper (zip/vector-zip []) 200)
"Elapsed time: 2488 msecs"
[[199 193 5 4 99 133 64 37 176 87 169 106 117 154 9 42 60 130 181 44 94 122 13 11 192 182 153 47 3 197 156 149 172 155 113 147 19 68 134 165 148 101 171 123 151 107 162 1 168 27 7 120 17 163 21 95 170 112 198 79 53 98 194 61 166 70 84 118 26 25 100 56 62 59 97 160 71 66 51 144 145 143 77 164 29 23 78 49 109 31 129 116 43 32 114 80 36 55 74 135 92 167 83 124 188 173 91 75 22 81 6 175 86 30 177 93 137 180 104 45 150 189 140 146 18 20 108 38 58 105 65 126 184 195 110 142 90 85 41 24 191 121 190 186 119 63 139 179 138 40 50 178 127 128 131 125 115 15 35 39 69 8 185 48 73 183 12 33 54 174 161 111 187 157 57 72 158 132 152 96 136 34 14 16 2 28 103 196 102 46 141 76 89 67 159 88 10 82 52 0] {:changed? true}]
ClojureScript:bench-insert> (bench insert-vector [] 200)
"Elapsed time: 1470 msecs"
[121 106 120 198 2 14 132 52 182 175 160 162 42 125 148 98 28 90 193 66 80 170 23 171 126 67 74 17 53 83 190 57 150 40 76 10 145 5 166 140 186 114 167 124 179 157 87 16 41 123 70 20 156 119 27 110 176 164 49 102 15 187 88 86 24 96 142 89 196 129 34 55 144 111 101 118 85 58 115 73 91 154 71 138 136 78 165 122 72 194 158 195 130 197 185 63 18 26 82 30 84 192 32 152 37 141 143 127 11 92 183 146 184 199 139 69 109 62 103 169 79 77 65 31 60 12 13 35 75 188 172 25 178 54 149 8 99 116 93 64 189 137 4 173 21 6 39 47 97 95 168 36 7 1 9 180 29 174 163 128 112 131 48 133 45 134 147 50 100 161 117 43 22 51 33 68 94 191 108 19 56 153 3 177 181 61 38 81 44 113 155 135 105 151 159 107 59 104 46 0]
ClojureScript:bench-insert>
(ns bench-insert
(:require [clojure.zip :as zip]))
(defn insert-zipper [n a z]
(if-let [l (zip/down z)]
(zip/up
(loop [n n l l]
(cond (zero? n) (zip/insert-left l a)
:else (if-let [r (zip/right l)]
(recur (dec n) r)
(zip/insert-right l a)))))
(zip/insert-child z a)))
(defn insert-vector [n a v]
(let [[x y] (split-at n v)]
(vec (concat x (list a) y))))
(defn insert-vector' [n x v]
(vec (doto (java.util.LinkedList. v) (.add n x))))
(defn bench [f z n]
(time
(reduce #(f (rand-int %2) %2 %1) z (range n))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment