56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
"""
|
|
Container ``in`` operator performance test
|
|
"""
|
|
import sys
|
|
import timeit
|
|
|
|
SETUP = '''
|
|
import array
|
|
selected = array.array('d')
|
|
with open('selected.arr', 'rb') as fp:
|
|
selected.fromfile(fp, {size})
|
|
if {container_type} is dict:
|
|
haystack = dict.fromkeys(selected, 1)
|
|
else:
|
|
haystack = {container_type}(selected)
|
|
if {verbose}:
|
|
print(type(haystack), end=' ')
|
|
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[::{size}//500])
|
|
if {verbose}:
|
|
print(' needles: %10d' % len(needles), end=' ')
|
|
'''
|
|
|
|
TEST = '''
|
|
found = 0
|
|
for n in needles:
|
|
if n in haystack:
|
|
found += 1
|
|
if {verbose}:
|
|
print(' found: %10d' % found)
|
|
'''
|
|
|
|
def test(container_type, verbose):
|
|
MAX_EXPONENT = 7
|
|
for n in range(3, MAX_EXPONENT + 1):
|
|
size = 10**n
|
|
setup = SETUP.format(container_type=container_type,
|
|
size=size, verbose=verbose)
|
|
test = TEST.format(verbose=verbose)
|
|
tt = timeit.repeat(stmt=test, setup=setup, repeat=5, number=1)
|
|
print(f'|{size:{MAX_EXPONENT + 1}d}|{min(tt):f}')
|
|
|
|
if __name__ == '__main__':
|
|
if '-v' in sys.argv:
|
|
sys.argv.remove('-v')
|
|
verbose = True
|
|
else:
|
|
verbose = False
|
|
if len(sys.argv) != 2:
|
|
print(f'Usage: {sys.argv[0]} <container_type>')
|
|
else:
|
|
test(sys.argv[1], verbose)
|