1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
.. _COMMON/Stopwatch:
Stopwatch
#########
.. #contents:: Table of Contents
:depth: 1
.. grid:: 2
.. grid-item::
:columns: 6
The stopwatch implements a solution to measure and collect timings: e.g. code execution times or test run times.
The time measurement can be :meth:`started <pyTooling.Stopwatch.Stopwatch.Start>`, :meth:`paused <pyTooling.Stopwatch.Stopwatch.Pause>`,
:meth:`resumed <pyTooling.Stopwatch.Stopwatch.Resume>` and :meth:`stopped <pyTooling.Stopwatch.Stopwatch.Stop>`. More
over, split times can be taken too. The measurement is based on :func:`time.perf_counter_ns`. Additionally, starting and
stopping is preserved as absolute time via :meth:`datetime.datetime.now`.
Every split time taken is a time delta to the previous stopwatch operation. These are preserved in an internal sequence
of splits. This sequence includes time deltas of activity and inactivity. Thus, a running stopwatch can be split as well
as a paused stopwatch.
The stopwatch can also be used in a :ref:`with-statement <with>`, because it implements the :ref:`context manager protocol <context-managers>`.
.. grid-item::
:columns: 6
.. tab-set::
.. tab-item:: Start/Stop
.. code-block:: Python
from pyTooling.Stopwatch import Stopwatch
sw = Stopwatch("my name")
sw.Start()
# do something
sw.Stop()
sw = Stopwatch("other name", started=True)
# do something
sw.Stop()
.. tab-item:: Start/Pause/Resume/Stop
.. code-block:: Python
from pyTooling.Stopwatch import Stopwatch
sw = Stopwatch("my name")
sw.Start()
# do something
sw.Pause()
# do something other
sw.Resume()
# do something again
sw.Stop()
.. tab-item:: Using with-statement
.. code-block:: Python
from pyTooling.Stopwatch import Stopwatch
sw = Stopwatch("my name", preferPause=True)
with sw:
# do something
# do something other
with sw
# do something again
.. _COMMON/Stopwatch/Features:
Features
********
.. grid:: 2
.. grid-item::
:columns: 6
Name
A stopwatch can be named at creation time.
Starting and stopping
The stopwatch can be started and stopped. Once stopped, no further start or pause/resume is possible. A
stopwatch can't be restarted. A new stopwatch object should be created and the old can be destroyed.
The stopwatch collects the absolute start (begin) and stop (end) times. It then provides a duration from start
to stop operation.
Pause and resume
A stopwatch can be paused and resumed.
Split times
tbd
Iterating split times
tbd
Using in a ``with``-statement
tbd
State of a stopwatch
tbd
.. grid-item::
:columns: 6
.. code-block:: Python
@export
class Stopwatch(SlottedObject):
def __init__(self, name: str = None, started: bool = False, preferPause: bool = False) -> None:
...
def __enter__(self) -> "Stopwatch":
...
def __exit__(self, exc_type: Type[Exception], exc_val: Exception, exc_tb: Traceback) -> bool:
...
def Start(self) -> None:
...
def Split(self) -> float:
...
def Pause(self) -> float:
...
def Resume(self) -> float:
...
def Stop(self):
...
@readonly
def Name(self) -> Nullable[str]:
...
@readonly
def IsStarted(self) -> bool:
...
@readonly
def IsRunning(self) -> bool:
...
@readonly
def IsPaused(self) -> bool:
...
@readonly
def IsStopped(self) -> bool:
...
@readonly
def StartTime(self) -> Nullable[datetime]:
...
@readonly
def StopTime(self) -> Nullable[datetime]:
...
@readonly
def HasSplitTimes(self) -> bool:
...
@readonly
def SplitCount(self) -> int:
...
@readonly
def ActiveCount(self) -> int:
...
@readonly
def InactiveCount(self) -> int:
...
@readonly
def Activity(self) -> float:
...
@readonly
def Inactivity(self) -> float:
...
@readonly
def Duration(self) -> float:
...
def __len__(self):
...
def __getitem__(self, index: int) -> Tuple[float, bool]:
...
def __iter__(self) -> Iterator[Tuple[float, bool]]:
...
|