import time import pyperf class A: pass class B: pass class D: pass class A_with_dunders: def __init__(self): pass def __repr__(self): return "" def __str__(self): return "" class B_with_dunders: def __init__(self): pass def __repr__(self): return "" def __str__(self): return "" class D_with_dunders: def __init__(self): pass def __repr__(self): return "" def __str__(self): return "" def bench_empty_cls(loops, value): ts = time.perf_counter() for _ in range(loops): for _ in range(value): class C: pass return time.perf_counter() - ts def bench_cls_with_dunders(loops, value): ts = time.perf_counter() for _ in range(loops): for _ in range(value): class C: def __init__(self): pass def __repr__(self): return "" def __str__(self): return "" return time.perf_counter() - ts def bench_empty_cls_with_base(loops, value): ts = time.perf_counter() for _ in range(loops): count, bases = value for _ in range(count): class C(*bases): pass return time.perf_counter() - ts def bench_cls_with_dunders_with_base(loops, value): ts = time.perf_counter() for _ in range(loops): count, bases = value for _ in range(count): class C(*bases): def __init__(self): pass def __repr__(self): return "" def __str__(self): return "" return time.perf_counter() - ts def bench(runner: pyperf.Runner, name, func, values=(1000,)): for value in values: runner.bench_time_func(f'{name}-{value}', func, value) def add_cmdline_args(cmd, args): cmd.append(args.xxx) if __name__ == '__main__': choices = { 'empty_cls', 'empty_cls_with_bases', 'empty_cls_with_bases_with_dunders', 'cls_with_dunders', 'cls_with_dunders_with_bases', 'cls_with_dunders_with_bases_with_dunders', } runner = pyperf.Runner(add_cmdline_args=add_cmdline_args) runner.argparser.add_argument('xxx', choices=['all']) args = runner.parse_args() runs = (1_000, 100_000) bases = ((A, B), (A, B, D)) dunders = ((A_with_dunders, B_with_dunders), (A_with_dunders, B_with_dunders, D_with_dunders)) if args.xxx == 'all': for run in runs: runner.bench_time_func(f'empty_cls-{run}', bench_empty_cls, run) runner.bench_time_func(f'cls_with_dunders-{run}', bench_cls_with_dunders, run) for b in bases + dunders: bname = ", ".join([_.__name__ for _ in b]) runner.bench_time_func(f'empty_cls_with_bases-{run}-{bname}', bench_empty_cls_with_base, (run, b)) runner.bench_time_func(f'cls_with_bases-{run}-{bname}', bench_cls_with_dunders_with_base, (run, b))