from time import perf_counter from importlib import import_module KB = 1024 MB = 1024*1024 GB = 1024*1024*1024 def benchmark(module, test_name, start, start_unit, end, end_unit, step, step_unit): m = import_module(module) compress = m.compress decompress = m.decompress print('imported:', module) unit_str = {KB:'KB', MB:'MB', GB:'GB'} fn = (f'{module}_{test_name}_' f'{end}{unit_str[end_unit]}_' f'{step}{unit_str[step_unit]}.txt') with open(fn, 'w') as f: for i in range(start*start_unit, end*end_unit, step*step_unit): dat = b'a' * i compressed = compress(dat) dat = None l = [] for _ in range(4): t1 = perf_counter() decompress(compressed) t2 = perf_counter() l.append(t2-t1) r = min(l) size = i // step_unit print(size, r) f.write('%d %f\n' % (size, r)) f.flush() name = 'baseline' benchmark('zlib', name, 0, MB, 20, MB, 64, KB) benchmark('zlib', name, 0, MB, 200, MB, 2, MB) benchmark('zlib', name, 0, MB, 2, GB, 30, MB) benchmark('bz2', name, 0, MB, 20, MB, 64, KB) benchmark('bz2', name, 0, MB, 200, MB, 2, MB) benchmark('bz2', name, 0, MB, 2, GB, 30, MB) benchmark('lzma', name, 0, MB, 20, MB, 64, KB) benchmark('lzma', name, 0, MB, 200, MB, 2, MB) benchmark('lzma', name, 0, MB, 2, GB, 30, MB) # import winsound # winsound.Beep(500, 2400)