-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
executable file
·330 lines (285 loc) · 9.32 KB
/
benchmark.py
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
#!/usr/bin/env python3
import click
import dataiter as di
import functools
import numpy as np
import random
import time
from dataiter import test
from statistics import mean
from unittest.mock import patch
@functools.cache
def _data_frame(path, nrow):
data = test.data_frame(path)
n = nrow // data.nrow
data = data.rbind(*([data] * n))
return data.head(nrow)
def data_frame(path, nrow=1_000_000):
return _data_frame(path, nrow).deepcopy()
@functools.cache
def _data_frame_random(nrows, ngroups):
return di.DataFrame(g=np.random.choice(ngroups, nrows, replace=True),
a=np.random.normal(10, 2, nrows))
def data_frame_random(nrows, ngroups):
return _data_frame_random(nrows, ngroups).deepcopy()
def data_frame_aggregate_128():
data = data_frame("vehicles.csv")
start = time.time()
(data
.group_by("make")
.aggregate(
n=di.count(),
hwy=di.mean("hwy"),
cty=di.mean("cty")))
return time.time() - start
def data_frame_aggregate_3264():
data = data_frame("vehicles.csv")
start = time.time()
(data
.group_by("make", "model")
.aggregate(
n=di.count(),
hwy=di.mean("hwy"),
cty=di.mean("cty")))
return time.time() - start
def data_frame_aggregate_14668():
data = data_frame("vehicles.csv")
start = time.time()
(data
.group_by("make", "model", "year")
.aggregate(
n=di.count(),
hwy=di.mean("hwy"),
cty=di.mean("cty")))
return time.time() - start
def data_frame_aggregate_100000_lambda():
data = data_frame_random(1_000_000, 100_000)
start = time.time()
(data
.group_by("g")
.aggregate(
a_mean=lambda x: np.mean(x.a),
a_std=lambda x: np.std(x.a)))
return time.time() - start
def data_frame_aggregate_100000_short():
with patch("dataiter.USE_NUMBA", False):
data = data_frame_random(1_000_000, 100_000)
start = time.time()
(data
.group_by("g")
.aggregate(
a_mean=di.mean("a"),
a_std=di.std("a")))
return time.time() - start
def data_frame_aggregate_100000_short_numba():
with patch("dataiter.USE_NUMBA", True):
data = data_frame_random(1_000_000, 100_000)
start = time.time()
(data
.group_by("g")
.aggregate(
a_mean=di.mean("a"),
a_std=di.std("a")))
return time.time() - start
def data_frame_full_join():
data = data_frame("vehicles.csv")
meta = data.select("make", "model").unique()
meta = meta.rbind(meta.modify(model="X"))
meta.random = np.random.random(meta.nrow)
assert meta.anti_join(data, "make", "model").nrow > 0
start = time.time()
data.full_join(meta, "make", "model")
return time.time() - start
def data_frame_left_join():
data = data_frame("vehicles.csv")
meta = data.select("make", "model").unique()
meta.random = np.random.random(meta.nrow)
start = time.time()
data.left_join(meta, "make", "model")
return time.time() - start
def data_frame_read_csv():
start = time.time()
test.data_frame("vehicles.csv")
return time.time() - start
def data_frame_read_json():
start = time.time()
test.data_frame("vehicles.json")
return time.time() - start
def data_frame_rbind_2():
# 2 * 500,000 = 1,000,000
data = data_frame("vehicles.csv", 500_000)
start = time.time()
data.rbind(data)
return time.time() - start
def data_frame_rbind_100():
# 100 * 10,000 = 1,000,000
data = data_frame("vehicles.csv", 10_000)
start = time.time()
data.rbind(*([data] * (100 - 1)))
return time.time() - start
def data_frame_rbind_100000():
# 100,000 * 10 = 1,000,000
data = data_frame("vehicles.csv", 10)
start = time.time()
data.rbind(*([data] * (100_000 - 1)))
return time.time() - start
def data_frame_sort():
data = data_frame("vehicles.csv")
start = time.time()
data.sort(make=1, model=1, year=1)
return time.time() - start
def data_frame_unique():
data = data_frame("vehicles.csv")
start = time.time()
data.unique("make", "model", "year")
return time.time() - start
def _list_of_dicts(path, length):
data = test.list_of_dicts(path)
n = length // len(data) + 1
data = data * n
return data.head(length)
@functools.cache
def list_of_dicts(path, length=100_000):
return _list_of_dicts(path, length).deepcopy()
def list_of_dicts_aggregate_128():
data = list_of_dicts("vehicles.json")
start = time.time()
(data
.group_by("make")
.aggregate(
n=len,
hwy=lambda x: mean(x.pluck("hwy")),
cty=lambda x: mean(x.pluck("cty"))))
return time.time() - start
def list_of_dicts_aggregate_3264():
data = list_of_dicts("vehicles.json")
start = time.time()
(data
.group_by("make", "model")
.aggregate(
n=len,
hwy=lambda x: mean(x.pluck("hwy")),
cty=lambda x: mean(x.pluck("cty"))))
return time.time() - start
def list_of_dicts_aggregate_14668():
data = list_of_dicts("vehicles.json")
start = time.time()
(data
.group_by("make", "model", "year")
.aggregate(
n=len,
hwy=lambda x: mean(x.pluck("hwy")),
cty=lambda x: mean(x.pluck("cty"))))
return time.time() - start
def list_of_dicts_full_join():
data = list_of_dicts("vehicles.json")
meta = data.deepcopy().select("make", "model").unique()
meta = meta + meta.deepcopy().modify(model=lambda x: "X")
meta = meta.modify(random=lambda x: random.random())
assert len(meta.anti_join(data, "make", "model")) > 0
start = time.time()
data.full_join(meta, "make", "model")
return time.time() - start
def list_of_dicts_left_join():
data = list_of_dicts("vehicles.json")
meta = data.deepcopy().select("make", "model").unique()
meta = meta.deepcopy().modify(random=lambda x: random.random())
start = time.time()
data.left_join(meta, "make", "model")
return time.time() - start
def list_of_dicts_read_csv():
start = time.time()
test.list_of_dicts("vehicles.csv")
return time.time() - start
def list_of_dicts_read_json():
start = time.time()
test.list_of_dicts("vehicles.json")
return time.time() - start
def list_of_dicts_sort():
data = list_of_dicts("vehicles.csv")
start = time.time()
data.sort(make=1, model=1, year=1)
return time.time() - start
def vector_fast_list():
seq = list(range(1_000_000))
start = time.time()
di.Vector.fast(seq, int)
return time.time() - start
def vector_fast_np_array():
seq = list(range(1_000_000))
seq = np.array(seq)
start = time.time()
di.Vector.fast(seq, int)
return time.time() - start
def vector_new_list():
seq = list(range(1_000_000))
start = time.time()
di.Vector(seq)
return time.time() - start
def vector_new_np_array():
seq = list(range(1_000_000))
seq = np.array(seq)
start = time.time()
di.Vector(seq)
return time.time() - start
def vector_rank_max():
data = data_frame("vehicles.csv")
start = time.time()
data.model.rank(method="max")
return time.time() - start
def vector_rank_min():
data = data_frame("vehicles.csv")
start = time.time()
data.model.rank(method="min")
return time.time() - start
def vector_rank_ordinal():
data = data_frame("vehicles.csv")
start = time.time()
data.model.rank(method="ordinal")
return time.time() - start
def vector_sort():
data = data_frame("vehicles.csv")
start = time.time()
data.model.sort()
return time.time() - start
def vector_unique():
data = data_frame("vehicles.csv")
start = time.time()
data.model.unique()
return time.time() - start
def is_benchmark(name):
prefixes = ("data_frame_", "list_of_dicts_", "vector_")
return name.startswith(prefixes) and name != "data_frame_random"
BENCHMARKS = sorted(filter(is_benchmark, dir()), key=lambda x: (
[x.zfill(9) if x.isdigit() else x for x in x.split("_")]))
def run_benchmarks(benchmarks, output, rounds):
width = max(map(len, benchmarks)) + 2
for i, benchmark in enumerate(benchmarks):
print(f"{i+1:2d}/{len(benchmarks)}. ", end="", flush=True)
print(f"{benchmark+' ':.<{width}} ", end="", flush=True)
try:
f = globals()[benchmark]
elapsed = 1000 * min(f() for i in range(rounds))
print("{:5.0f} ms".format(elapsed), flush=True)
except Exception as error:
elapsed = -1
print(error.__class__.__name__)
if not output: raise
yield {"name": benchmark, "elapsed": round(elapsed)}
@click.command()
@click.option("-o", "--output", help="Filename for optional CSV output")
@click.option("-r", "--rounds", default=5, help="Number of rounds per benchmark")
@click.option("--version", default=di.__version__, help="Version number for CSV output")
@click.argument("pattern", nargs=-1)
def main(output, rounds, version, pattern):
pattern = pattern or "_"
f = lambda x: any(y in x for y in pattern)
benchmarks = list(filter(f, BENCHMARKS))
results = di.ListOfDicts(run_benchmarks(benchmarks, output, rounds))
results = results.modify(version=lambda x: version)
if output:
assert output.endswith(".csv")
print(f"Writing {output}...")
results.write_csv(output)
if __name__ == "__main__":
main()