When your Python code is too slow, you need to identify the bottleneck that’s causing it: you need to understand what your code is doing. Luckily, beyond pre-existing profiling tools, there are also a variety of ways you can poke and prod Python programs to get a better understanding of what they’re doing internally.
This allows you to do one-time introspection, add profiling facilities to your program that you can turn on and off, build custom tools, and in general get a better understand of what your program is doing.
Some of these affordances are quite awful, but that’s OK! Performance debugging is a different kind of coding than writing long-term maintainable code.
In this article we’ll cover:
- Runtime object mutation (“monkey patching”).
- Code patching.
- Runtime mutation of C types.
- Audit hooks.
sys._current_frames()
.- Profiling and tracing hooks.
- And more!