Pyinstrument is a call stack profiler for Python that helps developers identify performance bottlenecks by visualizing which parts of their code consume the most execution time.
The tool works by sampling the call stack at regular intervals during program execution, then aggregating and displaying the results to show where time is actually being spent. Rather than instrumenting every function call, which adds overhead, pyinstrument uses statistical sampling to build an accurate picture of execution patterns with minimal performance impact on the profiled code itself.
Pyinstrument suits developers who need to optimize existing Python applications and want a straightforward way to locate slow functions without extensive code modification. It works well for profiling scripts, web applications, and async code. The tool integrates with Django through middleware and supports Jupyter notebooks via a magic command. It is particularly useful when you need to focus optimization efforts on the actual bottlenecks rather than guessing which functions to improve. The README does not compare it to alternative profilers.
The project maintains active development with recent releases addressing specific use cases: Django middleware customization for selective profiling and disk output, Jupyter magic command rendering options, and memory leak fixes in the C extension. The tool has expanded its build coverage to include Python prereleases and alternative implementations. Development activity includes refinements to output precision that adapt to sampling intervals, allowing users to see appropriate levels of detail depending on their profiling granularity.