I'm benchmarking some functions' memory in R and python, and had a great time getting results with the bench package in R that tracks all allocations by each call, allowing me to get the total and peak allocations by taking the sum and max. However, after much searching in python, I can't seem to find anything that can do it in the same way. I've been trying to use tracemalloc but it is severely underestimating. Here is a minimal example: import gc, tracemalloc def benchmark(func, iterations=10): gc.collect() tracemalloc.start() func() snapshot = tracemalloc.take_snapshot() tracemalloc.stop() stats = snapshot.statistics('lineno') func_stats = [stat for stat in stats if stat.traceback and func.__code__.co_filename in str(stat.traceback)] total_allocated_bytes = sum(stat.size for stat in func_stats) peak_allocated_bytes = max((stat.size for stat in func_stats), default=0) return total_allocated, peak_allocated However, this code doesn't work as intended because I get the following result: >>> benchmark(lambda: [i for i in range(1000000)]) (520, 280) >>> benchmark(lambda: [i for i in range(1000000)]) (64, 64) I don't know why the results are different, nor why it's telling me the call is using such little memory. What's wrong with the code? Or is there an alternative package that can allow an equivalent result to bench::bench_memory()? The actual code I want to benchmark will involve I/O and potentially multiple threads. Continue reading...