Getting Started
This section will help you install the library and basic usage of the stopwatch class.
Step. 1: Install the library.
bashpoetry add python-stopwatch2
poetry add python-stopwatch2
bashpython3 -m pip install python-stopwatch2
python3 -m pip install python-stopwatch2
bashpy -3 -m pip install python-stopwatch2
py -3 -m pip install python-stopwatch2
Step. 2: Import the Stopwatch class.
pythonfrom stopwatch import Stopwatch
from stopwatch import Stopwatch
Step. 3: Create a new Stopwatch object.
pythonsw = Stopwatch()
sw = Stopwatch()
Step. 4: Do what you want.
pythonfrom time import sleep sleep(2)
from time import sleep sleep(2)
Step. 5: Stop the stopwatch.
pythonsw.stop()
sw.stop()
Step. 6: Get the elapsed time.
pythonprint(f'Time elapsed: {sw.elapsed}') # Time elapsed: 2.0031827000002522 # or print(f'Time elapsed: {sw}') # Time elapsed: 2.00s
print(f'Time elapsed: {sw.elapsed}') # Time elapsed: 2.0031827000002522 # or print(f'Time elapsed: {sw}') # Time elapsed: 2.00s
Full example
pythonfrom stopwatch import Stopwatch from time import sleep sw = Stopwatch() sleep(2) sw.stop() print(f'Time elapsed: {sw}') # Time elapsed: 2.00s
from stopwatch import Stopwatch from time import sleep sw = Stopwatch() sleep(2) sw.stop() print(f'Time elapsed: {sw}') # Time elapsed: 2.00s