PlasmaCalcs.tools.timing.Stopwatch

class PlasmaCalcs.tools.timing.Stopwatch(printing=True)

Bases: object

tracks time since last clear (via self.reset()), or last “marked” time (via self.mark_time()).
All times are in seconds.
printing: bool, default True
whether to print self when exiting context, if self was used as a context manager.
Can also be used as a context manager, to time a block of code:
with Stopwatch() as watch:
>> code to time goes here <<
print(watch) # or, if watch.printing=True, this would already be printed, automatically.
Note that re-using an instance of Stopwatch as a context manager will reset it:
with Stopwatch() as watch:
>> code block 1 <<
watch.time # time it took to run code block 1
with watch: # <– calls watch.reset() when entering block.
>> code block 2 <<
watch.time # time it took to run code block 2
__init__(printing=True)

Methods

__init__([printing])

get_marked_time([key])

get_marked_times()

mark_time([key, _time])

now()

reset(*[, _time])

time_elapsed_string([as_string])

time_since_mark([key])

time_since_reset()

Attributes

time

time_elapsed

get_marked_time(key=None)
returns marked time associated with this key.
get_marked_times()
returns dict of marked times.
mark_time(key=None, *, _time=None)
sets “marked” time to now (or _time, if provided).
key: hashable object, default None
associate marked time with this key. (None –> “default” marked time.)
static now()
returns time.time(). Provided as a static method for convenience.
reset(*, _time=None)
sets “t=0” time to now (or _time, if provided). Also removes all “marked” times.
property time
alias to self.time_since_reset
property time_elapsed
alias to self.time_since_reset
time_elapsed_string(as_string=False)
returns string ‘Total time elapsed: {:.2f} seconds.’.format(self.time_since_reset())
time_since_mark(key=None)
returns time [in seconds] since the “mark” time on this watch.
“mark” set by self.mark_time(). Also set when created or reset().
key: hashable object, default None
“marked time” associated with this key.
time_since_reset()
returns time [in seconds] since the “t=0” time on this watch.
“t=0” set by self.reset(). Also set when created.