Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/bench/src/brython.py
1067 views
1
# The Tests from the Brython website https://brython.info/speed_results.html
2
3
# NOTE about Brython: I tried running these exact benchmarks in Brython itself on node.js
4
# via the node-bridge they provide, and it was overall 25x slower than
5
# python, with every single benchmark seemingly equally slow. This
6
# contradicts the performance claims on their website. I also saw this when
7
# benchmarking early on -- where the version of Brython on their demo page
8
# is much faster than anything I got when testing their releases, either on
9
# node or in the browser. I suspect they have a major performance regression.
10
11
from bench import register, all
12
13
14
def simple_assignment(n=1000000):
15
for i in range(n):
16
a = 1
17
assert a == 1
18
19
20
register('simple assignment', simple_assignment)
21
22
23
def augmented_assignment(n=1000000):
24
a = 0
25
for i in range(n):
26
a += 1
27
assert a == n
28
29
30
register('augmented_assignment', augmented_assignment)
31
32
33
def augmented_assignment_and_list_append(n=100000):
34
t = []
35
i = 0
36
while i < n:
37
t.append(i)
38
i += 1
39
assert len(t) == n
40
41
42
register('augmented_assignment_and_list_append',
43
augmented_assignment_and_list_append)
44
45
46
def simple_assignment_to_float(n=1000000):
47
for i in range(n):
48
a = 1.0
49
assert a == 1.0
50
51
52
register('simple_assignment_to_float', simple_assignment_to_float)
53
54
55
def big_integers(n=10000):
56
n = 60
57
for i in range(n):
58
2**n
59
60
61
register('big_integers', big_integers)
62
63
64
def build_dictionary(n=1000000):
65
for i in range(n):
66
a = {0: 0}
67
assert a == {0: 0}
68
69
70
register('build_dictionary', build_dictionary)
71
72
73
def build_dictionary_2(n=100000):
74
d = {}
75
for i in range(n):
76
d[i] = i
77
assert len(d) == n
78
79
80
register('build_dictionary_2', build_dictionary_2)
81
82
83
def set_dictionary_item(n=1000000):
84
a = {0: 0}
85
for i in range(n):
86
a[0] = i
87
assert a == {0: n - 1}
88
89
90
register('set_dictionary_item', set_dictionary_item)
91
92
93
def build_set(n=1000000):
94
for i in range(n):
95
a = {0, 2.7, "x"}
96
assert a == {0, 2.7, "x"}
97
98
99
register('build_set', build_set)
100
101
102
def build_list(n=1000000):
103
for i in range(n):
104
a = [1, 2, 3]
105
assert a == [1, 2, 3]
106
107
108
register("build_list", build_list)
109
110
111
def set_list_item(n=1000000):
112
a = [0]
113
for i in range(n):
114
a[0] = i
115
assert a[0] == n - 1
116
117
118
register("set_list_item", set_list_item)
119
120
121
def list_slice(n=100000):
122
a = [1, 2, 3]
123
for i in range(n):
124
a[:]
125
assert a[:] == [1, 2, 3]
126
127
128
register("list_slice", list_slice)
129
130
131
def integer_addition(n=1000000):
132
a, b, c = 1, 2, 3
133
for i in range(1000000):
134
a + b + c
135
assert a + b + c == 6
136
137
138
register("integer_addition", integer_addition)
139
140
141
def string_addition(n=1000000):
142
a, b, c = 'a', 'b', 'c'
143
for i in range(n):
144
a + b + c
145
assert a + b + c == 'abc'
146
147
148
register("string_addition", string_addition)
149
150
151
def cast_int_to_string(n=100000):
152
for i in range(n):
153
str(i)
154
155
156
register("cast_int_to_string", cast_int_to_string)
157
158
159
def create_function_without_arguments(n=1000000):
160
for i in range(n):
161
162
def f():
163
pass
164
165
166
register("create_function_without_arguments",
167
create_function_without_arguments)
168
169
170
def create_function_single_positional_argument(n=1000000):
171
for i in range(n):
172
173
def f(x):
174
pass
175
176
177
register("create_function_single_positional_argument",
178
create_function_single_positional_argument)
179
180
181
def create_function_complex_arguments(n=1000000):
182
for i in range(n):
183
184
def f(x, y=1, *args, **kw):
185
pass
186
187
188
register("create_function_complex_arguments",
189
create_function_complex_arguments)
190
191
192
def function_call(n=1000000):
193
def f(x):
194
return x
195
196
for i in range(n):
197
f(i)
198
199
assert f(1) == 1
200
201
202
register("function_call", function_call)
203
204
205
def function_call_complex_arguments(n=100000):
206
def f(x, y=0, *args, **kw):
207
return x
208
209
for i in range(n):
210
f(i, 5, 6, a=8)
211
assert f(10) == 10
212
213
214
register("function call, complex arguments", function_call_complex_arguments)
215
216
217
def create_simple_class(n=10000):
218
for i in range(n):
219
220
class A:
221
pass
222
223
224
register("create simple class", create_simple_class)
225
226
227
def create_class_with_init(n=10000):
228
for i in range(n):
229
230
class A:
231
def __init__(self, x):
232
self.x = x
233
234
235
register("create class with init", create_class_with_init)
236
237
238
def create_instance_of_simple_class(n=1000000):
239
class A:
240
pass
241
242
for i in range(n):
243
A()
244
245
246
register("create instance of simple class", create_instance_of_simple_class)
247
248
249
# The above timing is currently much worse in Jpython than python or pypy.
250
# This is because classes in Jpython are not written using ES6 classes, which
251
# will make this massively faster (i.e., 100x!)
252
def create_instance_of_simple_jsclass(n=1000000):
253
class A:
254
pass # so works in pure python
255
256
r"""%js
257
A = class A {
258
constructor() {}
259
__repr__() { return "I am a class"; }
260
}
261
"""
262
for i in range(n):
263
A()
264
265
266
register("create instance of simple jsclass",
267
create_instance_of_simple_jsclass)
268
269
270
def create_instance_of_class_with_init(n=100000):
271
class A:
272
def __init__(self, x):
273
self.x = x
274
275
for i in range(n):
276
A(i)
277
278
279
register("create instance of class with init",
280
create_instance_of_class_with_init)
281
282
283
def call_instance_method(n=100000):
284
class A:
285
def __init__(self, x):
286
self.x = x
287
288
def f(self):
289
return self.x
290
291
a = A(1)
292
for i in range(n):
293
a.f()
294
295
296
register("call_instance_method", call_instance_method)
297
298
if __name__ == '__main__':
299
all()
300
301