-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed_tests.py
417 lines (372 loc) · 10.4 KB
/
speed_tests.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python3
import sys
from platform import python_version
import time
import statistics
import types
import argparse
import array
from typing import List
class test:
def setup(self):
pass
def run(self):
pass
def test_class(cls):
cls._is_test = True
return cls
# --- TESTS ---
# Containers
class l_index_get(test):
def setup(self):
self.cycles = 1_000_000
def run(self):
buff, buff2 = self.buff, 0
for _ in range(self.cycles):
buff2 = buff[_]
class l_index_set(test):
def setup(self):
self.cycles = 1_000_000
def run(self):
buff = self.buff
for _ in range(self.cycles):
buff[_] = 0
@test_class
class list_index_get(l_index_get):
'buff = list[_]'
def setup(self):
super().setup()
self.buff = [1] * self.cycles
@test_class
class list_index_set(l_index_set):
'list[_] = 0'
def setup(self):
super().setup()
self.buff = [1] * self.cycles
@test_class
class array_index_get(l_index_get):
'buff = array[_]'
def setup(self):
super().setup()
self.buff = array.array('I', [1] * self.cycles)
@test_class
class array_index_set(l_index_set):
'array[_] = 0'
def setup(self):
super().setup()
self.buff = array.array('I', [1] * self.cycles)
@test_class
class memoryview_index_get(l_index_get):
'buff = memoryview[_]'
def setup(self):
super().setup()
self.buff = memoryview(array.array('I', [1] * self.cycles))
@test_class
class memoryview_index_set(l_index_set):
'memoryview[_] = 0'
def setup(self):
super().setup()
self.buff = memoryview(array.array('I', [1] * self.cycles))
@test_class
class bytearray_index_get(l_index_get):
'buff = bytearray[_]'
def setup(self):
super().setup()
self.buff = bytearray(self.cycles)
@test_class
class bytearray_index_set(l_index_set):
'bytearray[_] = 0'
def setup(self):
super().setup()
self.buff = bytearray(self.cycles)
@test_class
class tuple_index_get(l_index_get):
'buff = tuple[_]'
def setup(self):
super().setup()
self.buff = tuple(array.array('I', [1] * self.cycles))
@test_class
class dict_index_get(test):
'buff = dict[_]'
def setup(self):
self.cycles = 1_000_000
self.buff = {}
for i in range(self.cycles):
self.buff[i] = i
def run(self):
buff, buff2 = self.buff, 0
for _ in range(self.cycles):
buff2 = buff[_]
@test_class
class dict_index_set(test):
'dict[_] = 0'
def setup(self):
self.cycles = 1_000_000
self.buff = {}
for i in range(self.cycles):
self.buff[i] = i
def run(self):
buff = self.buff
for _ in range(self.cycles):
buff[_] = 0
# String
class string(test):
def setup(self):
self.cycles = 100_000
self.buff = ''
@test_class
class string_concatenate(string):
'buff += " "'
def run(self):
buff = self.buff
for _ in range(self.cycles):
buff += ' '
@test_class
class string_join(string):
'buff.join(" ")'
def run(self):
buff = self.buff
for _ in range(self.cycles):
buff = buff.join(' ')
@test_class
class string_format(string):
'buff.format(...)'
def run(self):
buff, buff2 = '{0} {1} {2}', ''
c1, c2 = 2, 3.0
for _ in range(self.cycles // 10):
buff2 = buff.format('1', c1, c2)
@test_class
class string_fstring(string):
'f"{buff} ..."'
def run(self):
buff = ''
c1, c2 = 2, 3.0
for _ in range(self.cycles // 10):
buff = f'{buff} {c1} {c2}'
@test_class
class string_cformat(string):
'"%..." % (...)'
def run(self):
buff = ''
c1, c2 = 2, 3.0
for _ in range(self.cycles // 10):
buff = '%s %i %f' % (buff, c1, c2)
# Class
@test_class
class class_attribute(test):
'class.attribute = 0'
class a:
pass
def setup(self):
self.cycles = 1_000_000
self.buff = self.a()
self.buff._attribute = 0
def run(self):
buff = self.buff
for _ in range(self.cycles):
buff._attribute = 0
@test_class
class class_slots_attribute(test):
'class.attribute = 0 (with __slots__)'
class a:
__slots__ = ('_attribute',)
def setup(self):
self.cycles = 1_000_000
self.buff = self.a()
self.buff._attribute = 0
def run(self):
buff = self.buff
for _ in range(self.cycles):
buff._attribute = 0
# Variable
@test_class
class assingment_multiple_lines(test):
'a = _; b = _'
def setup(self):
self.cycles = 10_000_000
def run(self):
a = 0
b = 0
for _ in range(self.cycles):
a = _
b = _
@test_class
class assingment_one_line(test):
'a, b = _, _'
def setup(self):
self.cycles = 10_000_000
def run(self):
a, b = 0, 1
for _ in range(self.cycles):
a, b = _, _
@test_class
class variables_self(test):
'self.a += 1'
def setup(self):
self.cycles = 10_000_000
self.a = 0
def run(self):
for _ in range(self.cycles):
self.a += 1
@test_class
class variables_local(test):
'a = self.a; a += 1'
def setup(self):
self.cycles = 10_000_000
self.a = 0
def run(self):
a = self.a
for _ in range(self.cycles):
a += 1
# Spetial cases
@test_class
class synthetic(test):
'synthetic'
class Context:
__slots__ = ('registers', 'breakpoints')
class Registers:
__slots__ = ('A', 'B', 'C', 'D', 'E', 'F', 'H', 'L', 'I', 'R', 'IX', 'IY', 'SP', 'PC')
def __init__(self):
self.A: int = 0
self.B: int = 0
self.C: int = 0
self.D: int = 0
self.E: int = 0
self.F: int = 0
self.H: int = 0
self.L: int = 0
self.I: int = 0
self.R: int = 0
self.IX: int = 0
self.IY: int = 0
self.SP: int = 0
self.PC: int = 0
def __str__(self):
return 'A={0.A:04x} B={0.B:04x} C={0.C:04x} D={0.D:04x} E={0.E:04x} F={0.F:04x} H={0.H:04x} L={0.L:04x} I={0.I:04x} R={0.R:04x} IX={0.IX:04x} IY={0.IY:04x} SP={0.SP:04x} PC={0.PC:04x}'.format(self)
class Breakpoint:
__slots__ = ('address', 'enabled')
def __init__(self):
self.address: int = 0
self.address_end: int = 0
self.enabled: bool = False
def is_triggered(self, address :int):
return self.address <= address <= self.address_end
def __init__(self):
self.registers: self.Registers = self.Registers()
self.breakpoints: List[self.Breakpoint] = []
def next_instruction(self, pc_advance: int):
self.registers.PC += pc_advance
class Instruction:
__slots__ = ('opcode', 'asm')
def __init__(self, opcode: int, asm: str):
'''opcode -- machine opcode
asm -- assembler mnemonic
'''
self.opcode, self.asm = opcode, asm
def execute(self, context):
pass
class mov_a_b(Instruction):
def __init__(self):
super().__init__(0xff, 'mov A, B')
def execute(self, context):
context.registers.A = context.registers.B
context.registers.PC
class inc_b(Instruction):
def __init__(self):
super().__init__(0x03, 'inc B')
def execute(self, context):
context.registers.B = (context.registers.B + 1) & 0xff
def setup(self):
self.cycles = 1_000_000
self.context = self.Context()
self.mov_a_b, self.inc_b = self.mov_a_b(), self.inc_b()
def run(self):
context, mov_a_b, inc_b = self.context, self.mov_a_b, self.inc_b
for _ in range(self.cycles):
mov_a_b.execute(context)
context.next_instruction(1)
inc_b.execute(context)
context.next_instruction(1)
# print(context.registers)
# --- END TESTS ---
def parse_args():
global log_level
parser = argparse.ArgumentParser(description='Test the python interpreter speed')
parser.add_argument('--csv', action='store_true', help='format results as csv (comma separated values). Do not use with -v option')
parser.add_argument('--header', action='store_true', help='header for csv')
parser.add_argument('-l', '--list', action='store_true', help='list availablere tests')
parser.add_argument('-v', action='count', default=0, help='verbose level: -v, -vv')
parser.add_argument('-g', '--group', nargs='*', help='run test group')
parser.add_argument('tests', metavar='[TEST1 [TEST2] ...]', nargs='*', help='space separated tests names. Use --list to list tests')
ret = parser.parse_args()
log_level = ret.v
return ret
available_tests = { x:getattr(sys.modules[__name__], x) for x in dir()
if isinstance(getattr(sys.modules[__name__], x), type) and issubclass(getattr(sys.modules[__name__], x), test) and hasattr(getattr(sys.modules[__name__], x), '_is_test') }
args = parse_args()
if args.list:
# list available tests
available_tests_names_max_len = max(len(x) for x in available_tests)
for i, kv in enumerate(available_tests.items()):
name, func = kv
print(f'{i+1:02} {name:{available_tests_names_max_len}} {func.__doc__ if func.__doc__ else name}')
else:
if not (args.csv or args.header):
print(f'{sys.version}')
if not args.tests:
# run all available tests
tests_names = available_tests.keys()
else:
# check for allowable tests
tests_names, wrong_names = [], []
for _ in args.tests:
if _.isdigit():
if int(_) > 0 and int(_) <= len(available_tests):
tests_names.append(tuple(available_tests.keys())[int(_) - 1])
else:
wrong_names.append(_)
elif _ not in available_tests:
wrong_names.append(_)
else:
tests_names.append(_)
if wrong_names:
print(f'Unknown tests: {", ".join(wrong_names)}')
print('Use --list to list tests')
sys.exit(1)
if args.group:
tests_names = list(filter(lambda x: any(x.startswith(xx+'_') for xx in args.group), tests_names))
# tests_names = [ _ for _ in tests_names if _.startswith(args.group+'_') ]
# run tests
def run_test(test_class, tests_count=7):
'runs one test for several times'
if args.csv:
print(f'{sys.implementation.name} {python_version()}{delimiter}{test_class.__name__}{delimiter}', end='')
else:
print(f'{test_class.__name__:{tests_names_max_len}}{delimiter}', end='\n' if log_level > 0 else '', flush=True)
time_results = [] # tests time measurements
for _ in range(tests_count):
if log_level > 0:
print(f'\ttest: {_+1}')
# prepare the test
tc = test_class()
tc.setup()
# run the test with time measurement
t = time.process_time()
tc.run()
elapsed_time = time.process_time() - t
time_results.append(elapsed_time)
# tests done # process the time measurements
if args.csv:
print(f'{statistics.mean(time_results):1.3}{delimiter}{min(time_results):1.3}{delimiter}{max(time_results):1.3}'
f'{delimiter}{max(time_results)-min(time_results):1.3}')
else:
print(f'avg={statistics.mean(time_results):1.3}{delimiter}min={min(time_results):1.3}{delimiter}max={max(time_results):1.3}'
f'{delimiter}delta={((max(time_results)-min(time_results))/max(time_results))*100:1.3}% ({max(time_results)-min(time_results):1.3})')
tests_names_max_len = max(len(x) for x in tests_names)
delimiter = ',' if args.csv or args.header else ' '
if args.header:
print(f'implementation{delimiter}name{delimiter}avg{delimiter}min{delimiter}max{delimiter}delta')
else:
for _ in tests_names:
run_test(available_tests[_])