In this video course, you’ve built up a Python timer step by step:
-
You used a class to keep state and add a user-friendly interface. Classes are very flexible, and using
Timerdirectly gives you full control over how and when to invoke the timer. -
You added support for multiple named timers, so you can measure several parts of your program independently.
-
You converted
Timerinto a data class to cut down on boilerplate and make the timer easier to configure. -
You used a context manager to add features to a block of code and, if necessary, to clean up afterward. Context managers are straightforward to use, and adding
with Timer()can help you more clearly distinguish your code visually.
You’ve also learned why you should prefer time.perf_counter() over time.time() when benchmarking code, as well as what other alternatives are useful when you’re optimizing your code.
Now you can add Python timer functions to your own code! Keeping track of how fast your program runs in your logs will help you monitor your scripts.
Resources mentioned in this lesson: