/
benchmark_runner.cljs
144 lines (128 loc) · 5.12 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
(ns cljs.benchmark-runner
(:refer-clojure :exclude [println])
(:require [cljs.reader :as reader]))
(def println print)
(set! *print-fn* js/print)
(simple-benchmark [x 1] (identity x) 1000000)
(println ";; array-reduce & ci-reduce")
(def arr (let [arr (array)]
(dotimes [i 1000000]
(.push arr i))
arr))
(defn sum [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 ";;; instance?")
;; WARNING: will get compiled away under advanced
(simple-benchmark [coll []] (instance? PersistentVector coll) 1000000)
(println ";;; satisfies?")
(simple-benchmark [coll (list 1 2 3)] (satisfies? ISeq coll) 1000000)
(simple-benchmark [coll [1 2 3]] (satisfies? ISeq coll) 1000000)
(println)
(println ";;; list ops")
(simple-benchmark [coll (list 1 2 3)] (first coll) 1000000)
(simple-benchmark [coll (list 1 2 3)] (-first coll) 1000000)
(simple-benchmark [coll (list 1 2 3)] (rest coll) 1000000)
(simple-benchmark [coll (list 1 2 3)] (-rest coll) 1000000)
(simple-benchmark [] (list) 1000000)
(simple-benchmark [] (list 1 2 3) 1000000)
(println)
(println ";;; vector ops")
(simple-benchmark [] [] 1000000)
(simple-benchmark [] [1 2 3] 1000000)
(simple-benchmark [coll [1 2 3]] (transient coll) 100000)
(simple-benchmark [coll [1 2 3]] (nth coll 0) 1000000)
(simple-benchmark [coll [1 2 3]] (-nth coll 0) 1000000)
(simple-benchmark [coll [1 2 3]] (conj coll 4) 1000000)
(simple-benchmark [coll [1 2 3]] (-conj coll 4) 1000000)
(simple-benchmark [coll [1 2 3]] (seq coll) 1000000)
(simple-benchmark [coll (seq [1 2 3])] (first coll) 1000000)
(simple-benchmark [coll (seq [1 2 3])] (-first coll) 1000000)
(simple-benchmark [coll (seq [1 2 3])] (rest coll) 1000000)
(simple-benchmark [coll (seq [1 2 3])] (-rest coll) 1000000)
(simple-benchmark [coll (seq [1 2 3])] (next coll) 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 ";;; reduce lazy-seqs, vectors, ranges")
(simple-benchmark [coll (take 100000 (iterate inc 0))] (reduce + 0 coll) 1)
(simple-benchmark [coll (range 1000000)] (reduce + 0 coll) 1)
(simple-benchmark [coll (into [] (range 1000000))] (reduce + 0 coll) 1)
(println)
(println ";;; map / record ops")
(simple-benchmark [coll {:foo 1 :bar 2}] (get coll :foo) 1000000)
(simple-benchmark [coll {:foo 1 :bar 2}] (-lookup coll :foo nil) 1000000)
(simple-benchmark [coll {:foo 1 :bar 2}] (:foo coll) 1000000)
(defrecord Foo [bar baz])
(simple-benchmark [coll (Foo. 1 2)] (:bar coll) 1000000)
(simple-benchmark [coll {:foo 1 :bar 2}] (assoc coll :baz 3) 100000)
(simple-benchmark [coll {:foo 1 :bar 2}] (assoc coll :foo 2) 100000)
(simple-benchmark [coll {:foo 1 :bar 2}]
(loop [i 0 m coll]
(if (< i 100000)
(recur (inc i) (assoc m :foo 2))
m))
1)
(println ";;; persistent hash maps")
(def pmap (into cljs.core.PersistentHashMap/EMPTY
[[:a 0] [:b 1] [:c 2] [:d 3] [:e 4] [:f 5] [:g 6] [:h 7]
[:i 8] [:j 9] [:k 10] [:l 11] [:m 12] [:n 13] [:o 14] [:p 15]
[:q 16] [:r 17] [:s 18] [:t 19] [:u 20] [:v 21] [:w 22] [:x 23]
[:y 24] [:z 25] [:a0 26] [:b0 27] [:c0 28] [:d0 29] [:e0 30] [:f0 31]]))
(simple-benchmark [key :f0] (hash key) 1000000)
(simple-benchmark [key :unsynchronized-mutable] (hash key false) 1000000)
(simple-benchmark [key :unsynchronized-mutable] (hash key) 1000000)
(def hash-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 pmap] (:f0 coll) 1000000)
(simple-benchmark [coll pmap] (get coll :f0) 1000000)
(simple-benchmark [coll pmap] (-lookup coll :f0 nil) 1000000)
(simple-benchmark [coll pmap] (assoc coll :g0 32) 1000000)
(simple-benchmark [coll pmap]
(loop [i 0 m coll]
(if (< i 1000000)
(recur (inc i) (assoc m :a 1))
m))
1)
(simple-benchmark [coll cljs.core.PersistentHashMap/EMPTY] (assoc coll :f0 1) 1000000)
(println)
(println ";;; set ops")
(simple-benchmark [] #{} 100000)
(simple-benchmark [] #{1 2 3} 100000)
(simple-benchmark [coll #{1 2 3}] (conj coll 4) 100000)
(println)
(println ";;; seq ops")
(simple-benchmark [coll (range 500000)] (reduce + coll) 1)
(println)
(println ";;; reader")
(simple-benchmark [s "{:foo [1 2 3]}"] (reader/read-string s) 1000)
(println)
(println ";;; range")
(simple-benchmark [r (range 1000000)] (last r) 1)
(println)
(defn ints-seq
([n] (ints-seq 0 n))
([i n]
(when (< i n)
(lazy-seq
(cons i (ints-seq (inc i) n))))))
(def r (ints-seq 1000000))
(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 "\n")