Summary
When it comes to performance, there is no holy grail, no single thing you can do to ensure peak performance in all cases. This shouldn’t worry you, however, as in most cases, you will never need to tune the performance and, if you do, a single tweak could probably fix your problem. You should be able to find performance problems and memory leaks in your code now, which is what matters most, so just try to contain yourself and only tweak when it’s actually needed.
Here is a quick recap of the tools in this chapter:
- Measuring CPU performance:
timeit
,profile
/cProfile
, andline_profiler
- Analyzing profiling results: SnakeViz,
pyprof2calltree
, and QCacheGrind - Measuring memory usage:
tracemalloc
,memory_profiler
- Reducing memory usage and leaks:
weakref
andgc
(garbage collector)
If you know how to use these tools, you should be able to track down and fix most performance issues in your code.
The most important takeaways...