timer

timer
is a library to time your Python code.
Installation
pip install timer4
Usage
timer
uses with
statement to watch how long your code running:
import time
from timer import Timer
with Timer().watch_and_report(msg='test'):
time.sleep(1.0)
- If you don't want to report the result immediately, use the
watch
method instead. Whenever you've done, call report
.
import time
from timer import Timer
total = 0
for item in range(7):
with Timer.get_instance().watch("sum of square"):
total += item ** 2
time.sleep(0.2)
time.sleep(0.8)
Timer.get_instance().report()
-
You can also use different way to print the message, such as using logging by passing a printing function to the report method: report(print_fn=logger.info)
-
You can also choose to append the result to a file report(append_to_file='/tmp/runtime.csv')
. This is useful if you want to measure runtime of your method and put it to a file to plot it later.