about summary refs log tree commit diff
path: root/third_party/immer/tools/clojure/src/immer_benchmark.clj
blob: f3d6f73e6c18e20d0c59955096532e74e260dc48 (plain) (blame)
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
(ns immer-benchmark
  (:require [criterium.core :as c]
            [clojure.core.rrb-vector :as fv]))

(defn h0 [& args]
  (apply println "\n####" args))

(defn h1 [& args]
  (apply println "\n====" args))

(defn h2 [& args]
  (apply println "\n----" args))

(defn run-benchmarks [N S]
  (h0 "Running benchmarks:  N =" N " S =" S)

  (def c-steps 10)

  (defn vector-push-f [v]
    (loop [v v
           i 0]
      (if (< i N)
        (recur (fv/catvec (fv/vector i) v)
               (inc i))
        v)))

  (defn vector-push [v]
    (loop [v v
           i 0]
      (if (< i N)
        (recur (conj v i)
               (inc i))
        v)))

  (defn vector-push! [v]
    (loop [v (transient v)
           i 0]
      (if (< i N)
        (recur (conj! v i)
               (inc i))
        (persistent! v))))

  (defn vector-concat [v vc]
    (loop [v v
           i 0]
      (if (< i c-steps)
        (recur (fv/catvec v vc)
               (inc i))
        v)))

  (defn vector-update [v]
    (loop [v v
           i 0]
      (if (< i N)
        (recur (assoc v i (+ i 1))
               (inc i))
        v)))

  (defn vector-update-random [v]
    (loop [v v
           i 0]
      (if (< i N)
        (recur (assoc v (rand-int N) i)
               (inc i))
        v)))

  (defn vector-update! [v]
    (loop [v (transient v)
           i 0]
      (if (< i N)
        (recur (assoc! v i (+ i 1))
               (inc i))
        (persistent! v))))

  (defn vector-update-random! [v]
    (loop [v (transient v)
           i 0]
      (if (< i N)
        (recur (assoc! v (rand-int N) i)
               (inc i))
        (persistent! v))))

  (defn vector-iter [v]
    (reduce + 0 v))

  (defn the-benchmarks [empty-v]
    (def full-v (into empty-v (range N)))
    (h2 "iter")
    (c/bench (vector-iter full-v) :samples S)
    (if (> N 100000)
      (h2 "skipping updates 'cuz N > 100000 (would run out of mem)")
      (do
        (h2 "update!")
        (c/bench (vector-update! full-v) :samples S)
        (h2 "update-random!")
        (c/bench (vector-update-random! full-v) :samples S)
        (h2 "push!")
        (c/bench (vector-push! empty-v) :samples S)
        (h2 "update")
        (c/bench (vector-update full-v) :samples S)
        (h2 "update-random")
        (c/bench (vector-update-random full-v) :samples S)
        (h2 "push")
        (c/bench (vector-push empty-v) :samples S))))

  (defn the-rrb-benchmarks [empty-v]
    (if (> N 1000)
      (h2 "skipping relaxed test 'cuz N > 1000 (rrb-vector bug)")
      (do
        (def full-v (vector-push-f empty-v))
        (h2 "iter/f")
        (c/bench (vector-iter full-v) :samples S)
        (h2 "update/f")
        (c/bench (vector-update full-v) :samples S)
        (h2 "update-random/f")
        (c/bench (vector-update-random full-v) :samples S)))
    (def short-v (into empty-v (range (/ N c-steps))))
    (h2 "concat")
    (c/bench (vector-concat empty-v short-v)) :samples S)

  (h1 "vector")
  (the-benchmarks (vector))

  (h1 "rrb-vector")
  (the-benchmarks (fv/vector))
  (the-rrb-benchmarks (fv/vector)))

(defn -main []
  (run-benchmarks 1000 100)
  (run-benchmarks 100000 20)
  (run-benchmarks 10000000 3))