A Stopwatch for timing blocks of code
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ez-stopwatch-1.0.1.tar.gz
.
File metadata
- Download URL: ez-stopwatch-1.0.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f2c69ed5f3a2d2dab347ddac491d5715397fc2418daeb97e0fa50b0caee9528 |
|
MD5 | 9da12a1edc3e0d4c196acafeb7c41db5 |
|
BLAKE2b-256 | 57c0cc0894055bcb9f1f0f0d12fe6f8de01e8a4fa4047bb72cde865ef1113072 |
File details
Details for the file ez_stopwatch-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: ez_stopwatch-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e20284a0aeed59de93da1b84975676ffb7275fa6b019c3934dace08932172969 |
|
MD5 | 9da1689779b802a650fe537b6d298014 |
|
BLAKE2b-256 | 68d01b48fb95b87df58037a23381de5e171dff981be8c23ebfaea623251006e0 |