import time import marshal def bench(func, *args): perf_counter = time.perf_counter times = [] for run in range(5): t0 = perf_counter() result = func(*args) dt = perf_counter() - t0 times.append(dt) return (result, min(times)) def bench_marshal(name, obj): print('') for version in range(2, 5): data, dt = bench(marshal.dumps, obj, version) _, lt = bench(marshal.loads, data) print('%-16s %2d %9.1f %9.1f %9.1f' % (name, version, dt*1e3, lt*1e3, len(data) / 1024)) def genData(amount=50000): for i in range(amount): yield (i, i+2, i*2, (i+1,i+4,i,4), "my string template %s" % i, 1.01*i, True) print('data ver. dumps(ms) loads(ms) size(KiB)') bench_marshal('genData', list(genData())) bench_marshal('[1000]*10**6', [1000] * 10**6) bench_marshal('[1000.0]*10**6', [1000.0] * 10**6) bench_marshal('[1000.0j]*10**6', [1000.0j] * 10**6) with open('Lib/_pydecimal.py') as f: src = f.read() data = [compile(src, '_pydecimal.py', 'exec') for i in range(20)] bench_marshal("20 pydecimals", data)