- Notifications
You must be signed in to change notification settings - Fork 790
/
Copy pathbenchmark_runner.cljs
451 lines (393 loc) · 18 KB
/
benchmark_runner.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
(nscljs.benchmark-runner
(:refer-clojure:exclude [println])
(:require [cljs.reader :as reader]
[clojure.core.reducers :as r]
[clojure.string :as string]))
(defprintlnprint)
(set! *print-fn* js/print)
(simple-benchmark [x 1] (identity x) 1000000)
(println";; symbol construction")
(simple-benchmark [] (symbol 'foo) 1000000)
(println)
(println";; array-reduce & ci-reduce")
(defarr (let [arr (array)]
(dotimes [i 1000000]
(.push arr i))
arr))
(defnsum [a b] (+ a b))
(simple-benchmark [coll (seq arr)] (ci-reduce coll + 0) 1)
(simple-benchmark [coll (seq arr)] (ci-reduce coll sum 0) 1)
(simple-benchmark [coll arr] (array-reduce coll + 0) 1)
(simple-benchmark [coll arr] (array-reduce coll sum 0) 1)
(println";; areduce")
(defx (atom0))
(simple-benchmark [arr (to-array (range1000000))] (reset! x (areduce arr i ret 0 (+ ret (aget arr i)))) 1)
(println";; amap")
(simple-benchmark [arr (to-array (range1000000))] (amap arr i ret (*10 (aget arr i))) 1)
(println";; js-keys")
(simple-benchmark [obj (js-obj"a"1"b"2) f js-keys] (f obj) 400000)
(simple-benchmark [obj (js-obj"a"1"b"2"c"3"d"4"e"5"f"6) f js-keys] (f obj) 400000)
(println";;; instance?")
;; WARNING: will get compiled away under advanced
(simple-benchmark [coll []] (instance? PersistentVector coll) 1000000)
(println";;; satisfies?")
(simple-benchmark [coll (list123)] (satisfies? ISeq coll) 1000000)
(simple-benchmark [coll [123]] (satisfies? ISeq coll) 1000000)
(println)
(println";;; array & string ops")
(simple-benchmark [coll (array123)] (seq coll) 1000000)
(simple-benchmark [coll "foobar"] (seq coll) 1000000)
(simple-benchmark [coll (array123)] (first coll) 1000000)
(simple-benchmark [coll "foobar"] (first coll) 1000000)
(simple-benchmark [coll (array123)] (nth coll 2) 1000000)
(simple-benchmark [coll "foobar"] (nth coll 2) 1000000)
(println)
(defprotocolIFoo (foo [x]))
(println";;; cloning & specify")
(simple-benchmark [coll [123]] (clone coll) 1000000)
(simple-benchmark [coll [123]] (specify coll IFoo (foo [_] :bar)) 1000000)
(simple-benchmark [coll (specify [123] IFoo (foo [_] :bar))] (foo coll) 1000000)
(println)
(println";;; list ops")
(simple-benchmark [coll (list123)] (first coll) 1000000)
(simple-benchmark [coll (list123)] (-first coll) 1000000)
(simple-benchmark [coll (list123)] (rest coll) 1000000)
(simple-benchmark [coll (list123)] (-rest coll) 1000000)
(simple-benchmark [] (list) 1000000)
(simple-benchmark [] (list123) 1000000)
(println)
(println";;; vector ops")
(simple-benchmark [] [] 1000000)
(simple-benchmark [[a b c] (take3 (repeatedly #(rand-int10)))] (-count [a b c]) 1000000)
(simple-benchmark [[a b c] (take3 (repeatedly #(rand-int10)))] (-count (vec #js [a b c])) 1000000)
(simple-benchmark [[a b c] (take3 (repeatedly #(rand-int10)))] (-count (vector a b c)) 1000000)
(simple-benchmark [coll [123]] (transient coll) 100000)
(simple-benchmark [coll [123]] (nth coll 0) 1000000)
(simple-benchmark [coll [123]] (-nth coll 0) 1000000)
(simple-benchmark [coll [123]] (-nth ^not-native coll 0) 1000000)
(simple-benchmark [coll [123]] (coll0) 1000000)
(simple-benchmark [coll [123]] (conj coll 4) 1000000)
(simple-benchmark [coll [123]] (-conj coll 4) 1000000)
(simple-benchmark [coll []] (-conj ^not-native coll 1) 1000000)
(simple-benchmark [coll [1]] (-conj ^not-native coll 2) 1000000)
(simple-benchmark [coll [12]] (-conj ^not-native coll 3) 1000000)
(simple-benchmark [coll [123]] (seq coll) 1000000)
(simple-benchmark [coll [123]] (-seq coll) 1000000)
(simple-benchmark [coll (seq [123])] (first coll) 1000000)
(simple-benchmark [coll (seq [123])] (-first coll) 1000000)
(simple-benchmark [coll (seq [123])] (rest coll) 1000000)
(simple-benchmark [coll (seq [123])] (-rest coll) 1000000)
(simple-benchmark [coll (seq [123])] (next coll) 1000000)
(println)
(println";;; large vector ops")
(simple-benchmark [] (reduce conj [] (range40000)) 10)
(simple-benchmark [coll (reduce conj [] (range (+3276832)))] (conj coll :foo) 100000)
(simple-benchmark [coll (reduce conj [] (range40000))] (assoc coll 123:foo) 100000)
(simple-benchmark [coll (reduce conj [] (range (+3276833)))] (pop coll) 100000)
(println)
(println";;; chunked seqs")
(let [v (seq (into [] (range64)))]
(simple-benchmark [] (-first v) 1000000)
(simple-benchmark [] (-next v) 1000000)
(simple-benchmark [] (-rest v) 1000000))
(println)
(println";;; transients")
(print"transient vector, conj! 1000000 items")
(time
(let [v (transient [])]
(loop [i 0 v v]
(if (> i 1000000)
(persistent! v)
(recur (inc i) (conj! v i))))))
(println";;; vector equality")
(simple-benchmark
[a (into [] (range1000000))
b (into [] (range1000000))]
(= a b) 1)
(println)
(println";;; keyword compare")
(let [seed ["amelia""olivia""jessica""emily""lily""ava""isla""sophie""mia""isabella""evie""poppy""ruby""grace""sophia""chloe""freya""isabelle""ella""charlotte""scarlett""daisy""lola""holly""eva""lucy""millie""phoebe""layla""maisie""sienna""alice""florence""lilly""ellie""erin""elizabeth""imogen""summer""molly""hannah""sofia""abigail""jasmine""matilda""megan""rosie""lexi""lacey""emma""amelie""maya""gracie""emilia""georgia""hollie""evelyn""eliza""amber""eleanor""bella""amy""brooke""leah""esme""harriet""anna""katie""zara""willow""elsie""annabelle""bethany""faith""madison""isabel""rose""julia""martha""maryam""paige""heidi""maddison""niamh""skye""aisha""mollie""ivy""francesca""darcey""maria""zoe""keira""sarah""tilly""isobel""violet""lydia""sara""caitlin"]]
(simple-benchmark
[arr (into-array (repeatedly10000 #(keyword (rand-nth seed))))]
(.sort arr compare)
100)
(simple-benchmark
[arr (into-array (repeatedly10000 #(keyword (rand-nth seed) (rand-nth seed))))]
(.sort arr compare)
100))
(println)
(println";;; reduce lazy-seqs, vectors, ranges")
(simple-benchmark [coll (take100000 (iterate inc 0))] (reduce + 0 coll) 1)
(simple-benchmark [coll (range1000000)] (reduce + 0 coll) 1)
(simple-benchmark [coll (into [] (range1000000))] (reduce + 0 coll) 1)
(println)
(println";; apply")
(simple-benchmark [coll (into [] (range1000000))] (apply + coll) 1)
(simple-benchmark [] (list12345) 1000000)
(simple-benchmark [xs (array-seq (array12345))] (apply list xs) 1000000)
(simple-benchmark [xs (list12345)] (apply list xs) 1000000)
(simple-benchmark [xs [12345]] (apply list xs) 1000000)
(simple-benchmark [f (fn [a b & more])] (apply f (range32)) 1000000)
(simple-benchmark [f (fn [a b c d e f g h i j & more])] (apply f (range32)) 1000000)
(println)
(println";; update-in")
(simple-benchmark [coll {:foo1} ks [:foo]] (update-in coll ks inc) 1000000)
(simple-benchmark [coll (array-map:foo1) ks [:foo]] (update-in coll ks inc) 1000000)
(println)
(println";;; obj-map")
(simple-benchmark [coll (obj-map)] (assoc coll :foo:bar) 1000000)
(simple-benchmark [coll (obj-map:foo:bar)] (-lookup coll :foo) 1000000)
(simple-benchmark [coll (obj-map:foo:bar)] (assoc coll :baz:woz) 1000000)
(simple-benchmark [coll (obj-map:foo:bar:baz:woz)] (-lookup coll :baz) 1000000)
(simple-benchmark [coll (obj-map:foo:bar:baz:woz:lol:rofl)] (-lookup coll :lol) 1000000)
(println)
(println";;; array-map")
(simple-benchmark [] {[1] true [2] true [3] true} 1000000)
(simple-benchmark [coll (array-map)] (assoc coll :foo:bar) 1000000)
(simple-benchmark [coll (array-map:foo:bar)] (-lookup coll :foo) 1000000)
(simple-benchmark [coll (array-map:foo:bar)] (assoc coll :baz:woz) 1000000)
(simple-benchmark [coll (array-map:foo:bar:baz:woz)] (-lookup coll :baz) 1000000)
(simple-benchmark [coll (array-map:foo:bar:baz:woz:lol:rofl)] (-lookup coll :lol) 1000000)
(println)
(println";;; array-map w/ symbols")
(let [a 'foo
b 'bar
c 'baz
d 'woz
e 'lol
f 'rofl]
(simple-benchmark [coll (array-map)] (assoc coll a b) 1000000)
(simple-benchmark [coll (array-map a b)] (-lookup coll a) 1000000)
(simple-benchmark [coll (array-map a b)] (assoc coll c d) 1000000)
(simple-benchmark [coll (array-map a b c d)] (-lookup coll c) 1000000)
(simple-benchmark [coll (array-map a b c d e f)] (-lookup coll e) 1000000))
(println)
(println";;; array-map w/ inline symbols")
(simple-benchmark [coll (array-map)] (assoc coll 'foo 'bar) 1000000)
(simple-benchmark [coll (array-map 'foo 'bar)] (-lookup coll 'foo) 1000000)
(simple-benchmark [coll (array-map 'foo 'bar)] (assoc coll 'baz 'woz) 1000000)
(simple-benchmark [coll (array-map 'foo 'bar 'baz 'woz)] (-lookup coll 'baz) 1000000)
(simple-benchmark [coll (array-map 'foo 'bar 'baz 'woz 'lol 'rofl)] (-lookup coll 'lol) 1000000)
(println)
(defdata-atom (atom {:x0}))
(println";;; map / record ops")
(simple-benchmark [coll {:foo1:bar2}] (get coll :foo) 1000000)
(simple-benchmark [coll {'foo 1 'bar 2}] (get coll 'foo) 1000000)
(simple-benchmark [coll {:foo1:bar2}] (-lookup coll :foonil) 1000000)
(simple-benchmark [coll {'foo 1 'bar 2}] (-lookup coll 'foo nil) 1000000)
(simple-benchmark [coll {:foo1:bar2}] (:foo coll) 1000000)
(simple-benchmark [coll {'foo 1 'bar 2}] ('foo coll) 1000000)
(let [kw :foo
sym 'foo]
(simple-benchmark [coll {:foo1:bar2}] (kw coll) 1000000)
(simple-benchmark [coll {'foo 1 'bar 2}] (sym coll) 1000000))
(simple-benchmark [coll {:foo1:bar2}]
(loop [i 0 m coll]
(if (< i 100000)
(recur (inc i) (assoc m :foo2))
m))
1)
(defrecordFoo [bar baz])
(simple-benchmark [coll (Foo.12)] (:bar coll) 1000000)
(simple-benchmark [coll (Foo.12)] (-lookup coll :bar) 1000000)
(simple-benchmark [coll (Foo.12)] (assoc coll :bar2) 1000000)
(simple-benchmark [coll (Foo.12)] (assoc coll :baz3) 1000000)
(simple-benchmark [coll (Foo.12)]
(loop [i 0 m coll]
(if (< i 1000000)
(recur (inc i) (assoc m :bar2))
m))
1)
(println)
(println";;; zipmap")
(simple-benchmark [m {:a1:b2:c3}] (zipmap (keys m) (map inc (vals m))) 100000)
(println)
(println";;; persistent hash maps")
(defpmap (into cljs.core.PersistentHashMap.EMPTY
[[:a0] [:b1] [:c2] [:d3] [:e4] [:f5] [:g6] [:h7]
[:i8] [:j9] [:k10] [:l11] [:m12] [:n13] [:o14] [:p15]
[:q16] [:r17] [:s18] [:t19] [:u20] [:v21] [:w22] [:x23]
[:y24] [:z25] [:a026] [:b027] [:c028] [:d029] [:e030] [:f031]]))
(simple-benchmark [key :f0] (hash key) 1000000)
(simple-benchmark [key "f0"] (m3-hash-unencoded-chars key) 1000000)
(simple-benchmark [key :unsynchronized-mutable] (hash key) 1000000)
(defhash-coll-test
(loop [i 0 r []]
(if (< i 1000)
(recur (inc i) (conj r (str"foo" i)))
r)))
(simple-benchmark [coll hash-coll-test] (hash-coll coll) 100)
(simple-benchmark [coll hash-coll-test] (hash-ordered-coll coll) 100)
(defhash-imap-test
(loop [i 0 r {}]
(if (< i 1000)
(recur (inc i) (conj r [(keyword (str"foo" i)) i]))
r)))
(defhash-imap-int-test
(loop [i 0 r {}]
(if (< i 1000)
(recur (inc i) (conj r [i i]))
r)))
(simple-benchmark [coll hash-imap-test] (hash-imap coll) 100)
(simple-benchmark [coll hash-imap-test] (hash-unordered-coll coll) 100)
(simple-benchmark [coll pmap] (:f0 coll) 1000000)
(simple-benchmark [coll pmap] (get coll :f0) 1000000)
(simple-benchmark [coll pmap] (-lookup coll :f0nil) 1000000)
(simple-benchmark [coll pmap] (-lookup ^not-native hash-imap-test :foo500nil) 1000000)
(simple-benchmark [coll pmap] (-lookup ^not-native hash-imap-int-test 500nil) 1000000)
(simple-benchmark [coll pmap] (assoc coll :g032) 1000000)
(simple-benchmark [coll pmap]
(loop [i 0 m coll]
(if (< i 1000000)
(recur (inc i) (assoc m :a1))
m))
1)
(simple-benchmark [coll cljs.core.PersistentHashMap.EMPTY] (assoc coll :f01) 1000000)
(println)
(print"transient map, conj! 100000 items")
(time
(let [m (transient cljs.core.PersistentHashMap.EMPTY)]
(loop [i 0 m m]
(if (> i 100000)
(persistent! m)
(recur (inc i) (assoc! m i i))))))
(println";;; set ops")
(simple-benchmark [] #{} 1000000)
(simple-benchmark [] #{123} 1000000)
(simple-benchmark [v [123]] (set v) 1000000)
(simple-benchmark [] (hash-set123) 1000000)
(simple-benchmark [coll #{123}] (conj coll 4) 1000000)
(simple-benchmark [coll #{123}] (get coll 2) 1000000)
(simple-benchmark [coll #{123}] (contains? coll 2) 1000000)
(simple-benchmark [coll #{123}] (coll2) 1000000)
(println)
(println";;; seq ops")
(simple-benchmark [coll (range500000)] (reduce + coll) 1)
(println)
(println";;; reader")
(defstrings
(take10 (iterate (fn [s] (str s "string")) "string")))
(defbig-str-data
(pr-str {:nils (repeat10nil)
:bools (concat (repeat5false) (repeat5true))
:ints (range1000010100)
:floats (map #(float (/ % 7)) (range0100))
:keywords (map keyword strings)
:symbols (map symbol strings)
:strings strings}))
(simple-benchmark [s "{:foo [1 2 3]}"] (reader/read-string s) 1000)
(simple-benchmark [s big-str-data] (reader/read-string s) 1000)
(println)
(println";;; range")
(simple-benchmark [r (range1000000)] (last r) 1)
(println)
(defnints-seq
([n] (ints-seq0 n))
([i n]
(when (< i n)
(lazy-seq
(cons i (ints-seq (inc i) n))))))
(defr (ints-seq1000000))
(println";;; lazy-seq")
(println";;; first run")
(simple-benchmark [r r] (last r) 1)
(println";;; second run")
(simple-benchmark [r r] (last r) 1)
(println)
(println";; iterators")
(defipmap (apply hash-map (range2000)))
(println";; Sequence iterator")
(simple-benchmark [s (seq ipmap)]
(let [iter (seq-iter s)]
(loop [v nil]
(if (.hasNext iter)
(recur (.next iter))
v)))
1000)
(println";; Direct iterator")
(simple-benchmark []
(let [iter (-iterator ipmap)]
(loop [v nil]
(if (.hasNext iter)
(recur (.next iter))
v)))
1000)
(println";;; comprehensions")
(simple-benchmark [xs (range512)] (last (for [x xs y xs] (+ x y))) 1)
(simple-benchmark [xs (vec (range512))] (last (for [x xs y xs] (+ x y))) 4)
(simple-benchmark [a (Box.0) xs (range512)] (doseq [x xs y xs] (set! a -val (+ (.-val a) x))) 4)
(simple-benchmark [a (Box.0) xs (vec (range512))] (doseq [x xs y xs] (set! a -val (+ (.-val a) x))) 4)
(println)
(println";; reducers")
(simple-benchmark [xs (into [] (range1000000))] (r/reduce + (r/map inc (r/map inc (r/map inc xs)))) 1)
(println";; transducers")
(simple-benchmark [xs (into [] (range1000000))] (transduce (comp (map inc) (map inc) (map inc)) + 0 xs) 1)
(println";; primitive array reduce 1000000 many ops")
(simple-benchmark [xs (into-array (range1000000))]
(-> xs (.map inc) (.map inc) (.map inc) (.reduce (fn [a b] (+ a b)) 0)) 1)
(println";; reduce range 1000000 many ops")
(simple-benchmark [xs (range1000000)] (reduce + 0 (map inc (map inc (map inc xs)))) 1)
(println";; transduce range 1000000 many ops ")
(simple-benchmark [xs (range1000000)] (transduce (comp (map inc) (map inc) (map inc)) + 0 xs) 1)
(println"\n")
(println";; multimethods")
(defmultisimple-multiidentity)
(defmethodsimple-multi:foo [x] x)
(simple-benchmark [] (simple-multi:foo) 1000000)
(println"\n")
(println";; higher-order variadic function calls")
;; Deliberately frustrates static-fn optimization and macros
(simple-benchmark [f array] (f1234567890) 100000)
(simple-benchmark [f vector] (f1234567890) 100000)
(simple-benchmark [] (=1111111110) 100000)
(println"\n")
(println";; Destructuring a sequence")
(simple-benchmark [v (into [] (range1000000))]
(loop [[x & xs] v]
(if-not (nil? xs)
(recur xs)
x))
10)
(println"\n")
(println";;; str")
(simple-benchmark [] (str1) 1000000)
(simple-benchmark [] (strnil) 1000000)
(simple-benchmark [] (str"1") 1000000)
(simple-benchmark [] (str"1""2") 1000000)
(simple-benchmark [] (str"1""2""3") 1000000)
(println"\n")
(println";;; clojure.string")
(simple-benchmark [s "a" f clojure.string/capitalize] (f s) 1000000)
(simple-benchmark [s "aBcDeF" f clojure.string/capitalize] (f s) 1000000)
(println";; printing of numbers and handling of ##Nan, ##Inf, ##-Inf")
(simple-benchmark [x true] (pr-str x) 1000000)
(simple-benchmark [x 10] (pr-str x) 1000000)
(simple-benchmark [x js/NaN] (pr-str x) 1000000)
(simple-benchmark [x js/Infinity] (pr-str x) 1000000)
(simple-benchmark [x js/-Infinity] (pr-str x) 1000000)
(simple-benchmark [x (js-obj)] (pr-str x) 1000000)
(println"\n")
(println";; cycle")
(simple-benchmark [] (doall (take1000 (cycle [123]))) 1000)
(simple-benchmark [] (into [] (take1000) (cycle [123])) 1000)
(simple-benchmark [] (reduce + (take64 (cycle [123]))) 10000)
(simple-benchmark [] (transduce (take64) + (cycle [123])) 10000)
(println"\n")
(println";; repeat")
(simple-benchmark [] (doall (take1000 (repeat1))) 1000)
(simple-benchmark [] (into [] (take1000) (repeat1)) 1000)
(simple-benchmark [] (doall (repeat10001)) 1000)
(simple-benchmark [] (into [] (repeat10001)) 1000)
(simple-benchmark [] (reduce + 0 (repeat10001)) 1000)
(simple-benchmark [] (into [] (take1000) (repeat1)) 1000)
(simple-benchmark [] (reduce + (take64 (repeat1))) 10000)
(simple-benchmark [] (transduce (take64) + (repeat1)) 10000)
(simple-benchmark [] (reduce + (take64 (repeat481))) 10000)
(simple-benchmark [] (transduce (take64) + (repeat481)) 10000)
(println"\n")
(println";; iterate")
(simple-benchmark [] (doall (take1000 (iterate inc 0))) 1000)
(simple-benchmark [] (into [] (take1000) (iterate inc 0)) 1000)
(simple-benchmark [] (reduce + (take64 (iterate inc 0))) 10000)
(simple-benchmark [] (transduce (take64) + (iterate inc 0)) 10000)
(println)