Timer for Python ⏳#
Why Use a Timer?#
Measuring time and performance should be easy. If you want to measure the time it takes to run Python programs and measure the performance of multiple blocks of code, Timer for Python is a lightweight package that does the job.
How It Works#
Basic Usage#
Simply wrap the Timer around a block of code that you want to measure:
Python | |
---|---|
After timer.stop()
, the elapsed time will be printed in the terminal:
Context Manager#
Alternatively, use the with
statement. This will automatically start and stop the clock – and so no need to declare timer.start()
and timer.stop()
. Same result as before, but less code:
How it appears in the terminal:
Multiple Threads#
Gain total flexibility to measure the performance of different blocks of code using multiple threads:
Python | |
---|---|
How it appears in the terminal:
% Elapsed time: 0.12345 seconds for thread B
% Elapsed time: 6.78 seconds for thread A
Function Decorator#
Or use @function_timer()
as function decorator to measure the performance of a function:
Python | |
---|---|
How it appears in the terminal:
% Elapsed time: 12.34 seconds for thread TEST_FUNCTION
Next Steps#
Ready to try? Let's get started.
Become a Sponsor
If you find this project helpful, please consider supporting its development. Your donations will help keep it alive and growing. Every contribution, no matter the size, makes a difference.
Thank you for your support! 🙌