update from Atlas with major reorg
This commit is contained in:
55
03-dict-set/support/container_perftest.py
Normal file
55
03-dict-set/support/container_perftest.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
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('|{:{}d}|{:f}'.format(size, MAX_EXPONENT + 1, min(tt)))
|
||||
|
||||
if __name__=='__main__':
|
||||
if '-v' in sys.argv:
|
||||
sys.argv.remove('-v')
|
||||
verbose = True
|
||||
else:
|
||||
verbose = False
|
||||
if len(sys.argv) != 2:
|
||||
print('Usage: %s <container_type>' % sys.argv[0])
|
||||
else:
|
||||
test(sys.argv[1], verbose)
|
||||
37
03-dict-set/support/container_perftest_datagen.py
Normal file
37
03-dict-set/support/container_perftest_datagen.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""
|
||||
Generate data for container performance test
|
||||
"""
|
||||
|
||||
import random
|
||||
import array
|
||||
|
||||
MAX_EXPONENT = 7
|
||||
HAYSTACK_LEN = 10 ** MAX_EXPONENT
|
||||
NEEDLES_LEN = 10 ** (MAX_EXPONENT - 1)
|
||||
SAMPLE_LEN = HAYSTACK_LEN + NEEDLES_LEN // 2
|
||||
|
||||
needles = array.array('d')
|
||||
|
||||
sample = {1/random.random() for i in range(SAMPLE_LEN)}
|
||||
print('initial sample: %d elements' % len(sample))
|
||||
|
||||
# complete sample, in case duplicate random numbers were discarded
|
||||
while len(sample) < SAMPLE_LEN:
|
||||
sample.add(1/random.random())
|
||||
|
||||
print('complete sample: %d elements' % len(sample))
|
||||
|
||||
sample = array.array('d', sample)
|
||||
random.shuffle(sample)
|
||||
|
||||
not_selected = sample[:NEEDLES_LEN // 2]
|
||||
print('not selected: %d samples' % len(not_selected))
|
||||
print(' writing not_selected.arr')
|
||||
with open('not_selected.arr', 'wb') as fp:
|
||||
not_selected.tofile(fp)
|
||||
|
||||
selected = sample[NEEDLES_LEN // 2:]
|
||||
print('selected: %d samples' % len(selected))
|
||||
print(' writing selected.arr')
|
||||
with open('selected.arr', 'wb') as fp:
|
||||
selected.tofile(fp)
|
||||
20
03-dict-set/support/hashdiff.py
Normal file
20
03-dict-set/support/hashdiff.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import sys
|
||||
|
||||
MAX_BITS = len(format(sys.maxsize, 'b'))
|
||||
print('%s-bit Python build' % (MAX_BITS + 1))
|
||||
|
||||
def hash_diff(o1, o2):
|
||||
h1 = '{:>0{}b}'.format(hash(o1), MAX_BITS)
|
||||
h2 = '{:>0{}b}'.format(hash(o2), MAX_BITS)
|
||||
diff = ''.join('!' if b1 != b2 else ' ' for b1, b2 in zip(h1, h2))
|
||||
count = '!= {}'.format(diff.count('!'))
|
||||
width = max(len(repr(o1)), len(repr(o2)), 8)
|
||||
sep = '-' * (width * 2 + MAX_BITS)
|
||||
return '{!r:{width}} {}\n{:{width}} {} {}\n{!r:{width}} {}\n{}'.format(
|
||||
o1, h1, ' ' * width, diff, count, o2, h2, sep, width=width)
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(hash_diff(1, 1.0))
|
||||
print(hash_diff(1.0, 1.0001))
|
||||
print(hash_diff(1.0001, 1.0002))
|
||||
print(hash_diff(1.0002, 1.0003))
|
||||
Reference in New Issue
Block a user