Exercises
Now that you have learned about many of the available tools for performance measuring and optimization, try and create a few useful decorators or context wrappers that will help you prevent issues:
- Try to create a decorator that monitors each run of a function and warns you if the memory usage grows each run.
- Try to create a decorator that monitors the runtime of a function and warns you if it deviates too much from the previous run. Optionally, you could make the function generate a (running) average runtime as well.
- Try to create a memory manager for your classes that warns you when more than a configured number of instances remain in memory. If you never expect more than 5 instances of a certain class, you can warn the user when that number is exceeded.
Example answers for these exercises can be found on GitHub: https://github.com/mastering-python/exercises. You are encouraged to submit your own solutions and learn about...