Modernize code to Python 3.6+ and some cleanup
This commit is contained in:
@@ -41,15 +41,15 @@ def test(container_type, verbose):
|
||||
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)))
|
||||
print(f'|{size:{MAX_EXPONENT + 1}d}|{min(tt):f}')
|
||||
|
||||
if __name__=='__main__':
|
||||
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])
|
||||
print(f'Usage: {sys.argv[0]} <container_type>')
|
||||
else:
|
||||
test(sys.argv[1], verbose)
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
Generate data for container performance test
|
||||
"""
|
||||
|
||||
import random
|
||||
import array
|
||||
import random
|
||||
|
||||
MAX_EXPONENT = 7
|
||||
HAYSTACK_LEN = 10 ** MAX_EXPONENT
|
||||
@@ -12,26 +12,26 @@ 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))
|
||||
sample = {1 / random.random() for i in range(SAMPLE_LEN)}
|
||||
print(f'initial sample: {len(sample)} elements')
|
||||
|
||||
# complete sample, in case duplicate random numbers were discarded
|
||||
while len(sample) < SAMPLE_LEN:
|
||||
sample.add(1/random.random())
|
||||
sample.add(1 / random.random())
|
||||
|
||||
print('complete sample: %d elements' % len(sample))
|
||||
print(f'complete sample: {len(sample)} elements')
|
||||
|
||||
sample = array.array('d', sample)
|
||||
random.shuffle(sample)
|
||||
|
||||
not_selected = sample[:NEEDLES_LEN // 2]
|
||||
print('not selected: %d samples' % len(not_selected))
|
||||
print(f'not selected: {len(not_selected)} samples')
|
||||
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(f'selected: {len(selected)} samples')
|
||||
print(' writing selected.arr')
|
||||
with open('selected.arr', 'wb') as fp:
|
||||
selected.tofile(fp)
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import sys
|
||||
|
||||
MAX_BITS = len(format(sys.maxsize, 'b'))
|
||||
print('%s-bit Python build' % (MAX_BITS + 1))
|
||||
print(f'{MAX_BITS + 1}-bit Python build')
|
||||
|
||||
def hash_diff(o1, o2):
|
||||
h1 = '{:>0{}b}'.format(hash(o1), MAX_BITS)
|
||||
h2 = '{:>0{}b}'.format(hash(o2), MAX_BITS)
|
||||
h1 = f'{hash(o1):>0{MAX_BITS}b}'
|
||||
h2 = f'{hash(o2):>0{MAX_BITS}b}'
|
||||
diff = ''.join('!' if b1 != b2 else ' ' for b1, b2 in zip(h1, h2))
|
||||
count = '!= {}'.format(diff.count('!'))
|
||||
count = f'!= {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)
|
||||
return (f'{o1!r:{width}} {h1}\n{" ":{width}} {diff} {count}\n'
|
||||
f'{o2!r:{width}} {h2}\n{sep}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(hash_diff(1, 1.0))
|
||||
|
||||
Reference in New Issue
Block a user