forked from fluentpython/example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_perftest.py
48 lines (44 loc) · 1.21 KB
/
set_perftest.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
"""
Set performance test
"""
import timeit
SETUP = '''
import array
selected = array.array('d')
with open('selected.arr', 'rb') as fp:
selected.fromfile(fp, {size})
haystack = {type}(selected)
# print('haystack: %10d' % len(haystack), end=' ')
needles = array.array('d')
with open('not_selected.arr', 'rb') as fp:
needles.fromfile(fp, 500)
needles.extend(selected[:500])
needles = set(needles)
# print(' needles: %10d' % len(needles), end=' ')
'''
tests = [
('FOR_LOOP_TEST', '''
found = 0
for n in needles:
if n in haystack:
found += 1
assert found == 500
'''),
('SET_&_TEST', '''
found = len(needles & haystack)
assert found == 500
'''
)]
MAX_EXPONENT = 7
for collection_type in 'dict.fromkeys set list'.split():
if collection_type == 'set':
available_tests = tests
else:
available_tests = tests[:1]
for test_name, test in available_tests:
print('*' * 25, collection_type, test_name)
for n in range(3, MAX_EXPONENT + 1):
size = 10**n
setup = SETUP.format(type=collection_type, size=size)
tt = timeit.repeat(stmt=test, setup=setup, repeat=5, number=1)
print('|{:{}d}|{:9.6f}'.format(size, MAX_EXPONENT + 1, min(tt)))