zero.time¶
Time management.
Timer¶
-
class
zero.time.Timer[source]¶ Measures time.
Measures time elapsed since the first call to
runup to “now” plus shift. The shift accumulates all pauses time and can be manually changed with the methodsaddandsub. If a timer is just created/reset, the shift is 0.0.Note
Measurements are performed via
time.perf_counter.Examples
timer = Timer()
Tutorial
import time assert Timer()() == 0.0 timer = Timer() timer.run() # start time.sleep(0.01) assert timer() # some time has passed timer.pause() elapsed = timer() time.sleep(0.01) assert timer() == elapsed # time didn't change because the timer is on pause timer.add(1.0) assert timer() == elapsed + 1.0 timer.run() # resume time.sleep(0.01) assert timer() > elapsed + 1.0 timer.reset() assert timer() == 0.0 with Timer() as timer: time.sleep(0.01) # timer is on pause and timer() returns the time elapsed within the context
Note
When a Timer instance is pickled, the result of
Timer.__call__is saved as shift (hence, the “time elapsed” is preserved) and all other attributes are omitted.
Reset the timer. |
|
Start/resume the timer. |
|
Pause the timer. |
|
|
Add non-negative delta to the shift. |
|
Subtract non-negative delta from the shift. |
Get time elapsed since the start. |
|
Measure time within a context. |
|
|
Leave the context and pause the timer. |
functions¶
|
Format numeric seconds in a human-readable string. |