Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/bench/src/mypyc_micro.py
1067 views
1
# Selected microbenchmarks from the mypyc project.
2
# These are interesting little benchmarks, similar to the brython ones, but better
3
# See https://github.com/mypyc/mypyc-benchmarks/tree/master/microbenchmarks
4
# We are only doing benchmarks here that are easy to express the
5
# python language, e.g., some that involve a bunch of string methods aren't really
6
# supported. E.g., pylang doesn't have string % tuple yet.
7
8
from bench import register, all
9
from typing import Iterator, List
10
11
12
def is_close(x: float, y: float) -> bool:
13
return 0.999999 <= x / y <= 1.000001
14
15
16
# pylang gets killed in this generators one:
17
18
19
def generators() -> None:
20
n = 0
21
k = 0
22
for i in range(100 * 1000):
23
for j in gen(k):
24
n += j
25
k += 1
26
if k == 10:
27
k = 0
28
assert n == 1200000, n
29
30
31
def gen(n: int) -> Iterator[int]:
32
for i in range(n):
33
yield i
34
35
36
register("mypyc - generators", generators)
37
38
39
def str_slicing() -> None:
40
a = []
41
for i in range(1000):
42
a.append(f'Foobar-{i}')
43
a.append(f'{i} str')
44
45
n = 0
46
for i in range(1000):
47
for s in a:
48
n += len(s[2:-2])
49
if s[:3] == 'Foo':
50
n += 1
51
if s[-2:] == '00':
52
n += 1
53
assert n == 9789000, n
54
55
56
register("str_slicing", str_slicing)
57
58
59
def ord_builtin() -> None:
60
a = []
61
for i in range(1000):
62
a.append(f'Foobar-{i}')
63
a.append(f'{i}-ab-asdfsdf-asdf')
64
a.append('yeah')
65
n = 0
66
for i in range(50):
67
for s in a:
68
for j in range(len(s)):
69
if 97 <= ord(s[j]) <= 122:
70
n += 1
71
if is_upper_case_letter(s[j]):
72
n += 2
73
if s[j] == ord('a'):
74
n += 3
75
assert n == 1200000, n
76
77
78
def is_upper_case_letter(ch: str) -> bool:
79
return 65 <= ord(ch) <= 90
80
81
82
register('ord_builtin', ord_builtin)
83
84
85
def matrix_multiply() -> None:
86
"""Naive matrix multiplication benchmark."""
87
SIZE = 30
88
SEED = 535
89
import random
90
91
def setup_matrix_mult():
92
def make_matrix(w: int, h: int):
93
result = []
94
for i in range(h):
95
result.append([random.random() for _ in range(w)])
96
return result
97
98
return make_matrix(SIZE, SIZE), make_matrix(SIZE, SIZE)
99
100
def multiply(a, b):
101
result = []
102
for i in range(len(a)):
103
result.append([0.0] * len(b[0]))
104
for j in range(len(b[0])):
105
x = 0.0
106
for k in range(len(b)):
107
x += a[i][k] * b[k][j]
108
result[-1][j] = x
109
return result
110
111
m, m2 = setup_matrix_mult()
112
for i in range(50):
113
m = multiply(m, m2)
114
115
116
register("matrix multiplication", matrix_multiply)
117
118
119
def int_to_float() -> None:
120
a = [1, 4, 6, 7, 8, 9]
121
x = 0.0
122
for i in range(1000 * 1000):
123
for n in a:
124
x += float(n)
125
assert x == 35000000.0, x
126
127
128
register('int_to_float', int_to_float)
129
130
131
def str_to_float() -> None:
132
a = ['1', '1.234567', '44324', '23.4', '-43.44e-4']
133
x = 0.0
134
for i in range(1000 * 1000):
135
for n in a:
136
x += float(n)
137
assert is_close(x, 44349630223.26009), x
138
139
140
register('str_to_float', str_to_float)
141
142
143
def float_abs() -> None:
144
a = [1, -1.234567, 44324, 23.4, -43.44e-4]
145
x = 0.0
146
for i in range(1000 * 1000):
147
for n in a:
148
x += abs(n)
149
assert is_close(x, 44349638911.052574), x
150
151
152
register("float_abs", float_abs)
153
154
155
def int_divmod() -> None:
156
a = [1, 1235, 5434, 394879374, -34453]
157
n = 0
158
for i in range(1000 * 1000):
159
for x in a:
160
q, r = divmod(x, 23)
161
n += q + r
162
assert n == 17167493000000, n
163
164
165
register("int_divmod", int_divmod)
166
167
168
def int_list() -> None:
169
a = list(range(200))
170
b = list(reversed(a))
171
c = [-1, 3, 7, 1234] * 40
172
n = 0
173
for i in range(4000):
174
n += sum_ints(a)
175
n += min_int(a)
176
n += min_int(b)
177
n += sum_ints(b)
178
n += sum_ints(c)
179
n += min_int(c)
180
assert n == 358076000, n
181
182
183
register("int_list", int_list)
184
185
186
def sum_ints(a: List[int]) -> int:
187
s = 0
188
for x in a:
189
s += x
190
return s
191
192
193
def min_int(a: List[int]) -> int:
194
minimum = a[0]
195
for i in range(1, len(a)):
196
x = a[i]
197
if x < minimum:
198
minimum = x
199
return minimum
200
201
202
def int_bitwise_ops() -> None:
203
a = []
204
for i in range(1000):
205
a.append(i * i * 12753 % (2**20 - 1))
206
b = a[10:50]
207
208
n = 0
209
210
for i in range(50):
211
for j in a:
212
for k in b:
213
j |= k
214
j &= ~(j ^ k)
215
x = j >> 5
216
n += x
217
n += x << 1
218
n &= 0xffffff
219
220
assert n == 4867360, n
221
222
223
register("int_bitwise_ops", int_bitwise_ops)
224
225
# We don't have arb precision yet
226
"""
227
def int_long_bitwise_ops() -> None:
228
a = []
229
for i in range(1000):
230
a.append(i * i ** (i // 15))
231
b = a[10:500:10]
232
print(b)
233
n = 0
234
235
for i in range(10):
236
for j in a:
237
for k in b:
238
j |= k
239
j &= ~(j ^ k)
240
if (1 << (i * 19)) & j:
241
n += 1
242
n += j & 1
243
print(n)
244
assert n == 122000, n
245
246
register("int_long_bitwise_ops", int_long_bitwise_ops)
247
"""
248
249
250
def list_of_dicts():
251
a = []
252
for j in range(1000):
253
d = {}
254
for i in range(j % 10):
255
d[f'Foobar-{j}'] = j
256
d[f'{j} str'] = i
257
# dict = so we get keys, values below in pylang
258
a.append(dict(d))
259
return a
260
261
262
def dict_iteration() -> None:
263
a = list_of_dicts()
264
265
n = 0
266
for i in range(1000):
267
for d in a:
268
for k in d:
269
if k == '0 str':
270
n += 1
271
for k in d.keys():
272
if k == '0 str':
273
n += 1
274
for v in d.values():
275
if v == 0:
276
n += 1
277
for k, v in d.items():
278
if v == 1 or k == '1 str':
279
n += 1
280
assert n == 202000, n
281
282
283
register("dict_iteration", dict_iteration)
284
285
286
def dict_to_list() -> None:
287
a = list_of_dicts()
288
289
n = 0
290
for i in range(100):
291
for d in a:
292
n += len(list(d.keys()))
293
n += len(list(d.values()))
294
n += len(list(d.items()))
295
assert n == 540000, n
296
297
298
register("dict_to_list", dict_to_list)
299
300
# def dict_clear() -> None:
301
# n = 0
302
# for i in range(1000 * 100):
303
# d = {}
304
# for j in range(i % 4):
305
# d[j] = 'x'
306
# d.clear()
307
# assert len(d) == 0
308
309
# register("dict_clear", dict_clear)
310
311
312
def dict_copy() -> None:
313
a = list_of_dicts()
314
315
n = 0
316
for i in range(100):
317
for d in a:
318
d2 = d.copy()
319
d3 = d2.copy()
320
d4 = d3.copy()
321
assert len(d4) == len(d)
322
323
324
register("dict_copy", dict_copy)
325
326
if __name__ == '__main__':
327
all()
328
329