Skip to main content
# >>> from withstopwatch import Stopwatch

Basic usage:
>>> with Stopwatch():
...     print('<some activity to be measured>')
<some activity to be measured>
0 ms

Fields:
>>> with Stopwatch() as s:
...     pass
0 ms
>>> assert isinstance(s.label, str) and s.label == '0 ms'
>>> assert isinstance(s.ms, int) and s.ms == 0
>>> assert isinstance(s.s, int) and s.s == 0
>>> assert isinstance(s.seconds, float) and s.seconds > 0
>>> assert isinstance(s.timedelta, datetime.timedelta)
>>> assert isinstance(s.start, datetime.datetime)
>>> assert isinstance(s.stop, datetime.datetime)
>>> assert s.stop - s.start == s.timedelta

`str` and `repr`:
>>> assert str(s) == s.label
>>> repr(s)  # doctest: +ELLIPSIS
'<Stopwatch: datetime.timedelta(...)>'

Silencing:
>>> with Stopwatch(template=None) as s:
...     pass
>>> assert s.label is None
>>> with Stopwatch(print=None) as s:
...     pass
>>> assert s.label == '0 ms'

Special print:
>>> with Stopwatch(print=lambda line: print('DEBUG:', line)) as s:
...     pass
DEBUG: 0 ms

Templates:
>>> with Stopwatch(template='passing'):
...     pass
0 ms: passing
>>> with Stopwatch('passing'):
...     pass
0 ms: passing
>>> with Stopwatch('{ms} ms'):
...     pass
0 ms
>>> with Stopwatch('{s} s'):
...     pass
0 s
>>> with Stopwatch('{:.03f} (i.e. {seconds:.03f})'):
...     pass
0.000 (i.e. 0.000)
>>> with Stopwatch('{timedelta}'):
...     pass   # doctest: +ELLIPSIS
0:00:00...
>>> with Stopwatch('from {start!r} to {stop!r}') as s:
...     pass   # doctest: +ELLIPSIS
from datetime.datetime(...) to datetime.datetime(...)

With an exception (note: due to limitations of `doctest`, we can't check
easily both that something is printed and that an exception is raised):
>>> with Stopwatch(print=None):
...     raise Exception()
Traceback (most recent call last):
Exception
>>> try:
...     with Stopwatch() as s:
...        raise Exception()
... except Exception as exc:
...     print(repr(s))  # doctest: +ELLIPSIS
0 ms (failed)
<Stopwatch: datetime.timedelta(...)>
>>> try:
...     with Stopwatch('abc') as s:
...        raise Exception()
... except Exception as exc:
...     print(repr(s))  # doctest: +ELLIPSIS
0 ms: abc (failed)
<Stopwatch: datetime.timedelta(...)>

Testing with `time.sleep` (note: the tests may fail during a slower run):
>>> import time
>>> with Stopwatch() as s:
...     time.sleep(0.1)
100 ms
>>> assert 0.1 < s.seconds < 0.2

Embedded stopwatches:
>>> with Stopwatch('outer'):
...     with Stopwatch('inner'):
...         pass
0 ms: inner
0 ms: outer

Measuring the overhead of Stopwatch using Stopwatch itself
(note: the tests may fail during a slower run):
>>> with Stopwatch(print=None) as s1:
...     with Stopwatch('inner') as s2:
...         pass
0 ms: inner
>>> with Stopwatch(print=None) as s3:
...     pass
>>> overhead = s1.seconds - s3.seconds
>>> assert 0 <= overhead < 0.0001
... # ~20 us on i5@2.60GHz, Python 3.6.0, Linux

Metadata

Release files for withstopwatch 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for withstopwatch 0.3.0
File Size Uploaded
withstopwatch-0.3.0.tar.gz 2.6 kB Details

Release files / withstopwatch-0.3.0.tar.gz

Download URL withstopwatch-0.3.0.tar.gz
Size 2.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c9f2e75fcd22e611bc2812f46b5a9656129a31eedb4b9a5e3cc0ffd07e8ac948
BLAKE2b-256 checksum
How to use checksums
79c162f1c5e0f9f0bd5b060e9026819d9efa6ad9cf43bc7c203c31b525e29add
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.1

Release history Release notifications | RSS feed

This release

0.3.0 This release

1 release file

0.2.0

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.14

1 release file

0.0.13

1 release file

0.0.12

1 release file

0.0.11

1 release file

0.0.10

1 release file

0.0.9

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page