Timer module¶
Utilities to time code:
- a
Timer
class that can be used as a context manager - a
timeit
decorator for functions.
Timer
¶
A timer that can be started, stopped, and reset as needed by the user.
It keeps track of the total elapsed time in the elapsed
attribute::
Examples:
>>> with Timer() as t:
>>> ....
>>> print(f"... took {t.elapsed} seconds")
use Timer(time.process_time)
to get only CPU time.
can also do:
Examples:
>>> t = Timer()
>>> t.start()
>>> t.stop()
>>> t.start() # will add to the same counter
>>> t.stop()
>>> print(f"{t.elapsed} seconds total")
Source code in bs_python_utils/Timer.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
timeit(func)
¶
Decorator to time a function
Source code in bs_python_utils/Timer.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|