Python Stopwatch
This module contains a Stopwatch class with a context manager and a decorator function to time your code easily
Below are some examples:
First import the Stopwatch class
from stopwatch import Stopwatch
For the tutorial we're going to use this function which takes a bit more than a second to run
def func():
x = ''
for i in range(1_000_000):
x += str(i)
Instantiate the Stopwatch class:
sw = Stopwatch()
sw.start()
func()
sw.stop()
print('Time elapsed:', sw.time_elapsed)
Time elapsed: 1.2138440999988234
Or we can simply print the instance which will return a nicely formatted string:
print(sw)
Time elapsed: 1.214
Using the 'with' statement:
with Stopwatch() as sw:
func()
print(sw)
Time elapsed: 1.138
You can also pass an instance of a class to automatically add the time taken to the total elapsed time of the instance:
sw = Stopwatch()
with sw:
func()
func()
with sw:
func()
print(sw)
Time elapsed: 2.328
The above example is the equivalent of:
sw = Stopwatch()
sw.start()
func()
sw.stop()
func()
sw.start()
func()
sw.stop()
print(sw)
Time elapsed: 2.324
You can also use the decorator to time a function every time you run it
First import the decorator:
from stopwatch import time_it
Add it to your function:
@time_it()
def func():
x = ''
for i in range(1_000_000):
x += str(i)
Now the function will return a tuple with the time taken and the output of the function (which in our case is None)
print(func())
(1.1769665000028908, None)
You can also add the time taken to an existing Stopwatch instance like so:
sw = Stopwatch()
@time_it(stopwatch=sw)
def func():
x = ''
for i in range(1_000_000):
x += str(i)
Call the function twice:
print('Out:', func())
print(sw)
print('Out:', func())
print(sw)
Out: (1.2755557999989833, None)
Time elapsed: 1.276
Out: (1.3011283000014373, None)
Time elapsed: 2.577
Metadata
Release files for ez-stopwatch 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ez-stopwatch-1.0.1.tar.gz | 4.4 kB | Without provenance | Details for ez-stopwatch-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ez_stopwatch-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for ez_stopwatch-1.0.1-py3-none-any.whl |
Total release size: 9.5 kB
Release files / ez-stopwatch-1.0.1.tar.gz
| Download URL | ez-stopwatch-1.0.1.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9f2c69ed5f3a2d2dab347ddac491d5715397fc2418daeb97e0fa50b0caee9528
|
|
BLAKE2b-256 checksum How to use checksums |
57c0cc0894055bcb9f1f0f0d12fe6f8de01e8a4fa4047bb72cde865ef1113072
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.2
|
Release files / ez_stopwatch-1.0.1-py3-none-any.whl
| Download URL | ez_stopwatch-1.0.1-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e20284a0aeed59de93da1b84975676ffb7275fa6b019c3934dace08932172969
|
|
BLAKE2b-256 checksum How to use checksums |
68d01b48fb95b87df58037a23381de5e171dff981be8c23ebfaea623251006e0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.2
|
Security for ez-stopwatch 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.