A Simple Python Stopwatch

2.0.1 · active · verified Fri Apr 17

stopwatch-py is a lightweight Python library providing a simple stopwatch functionality to measure elapsed time. It is currently at version 2.0.1 and has a low release cadence, primarily focusing on minor improvements and maintenance.

Common errors

Warnings

Install

Imports

Quickstart

Initializes a stopwatch, measures time, stops and resumes, then resets it.

from stopwatch import Stopwatch
import time

s = Stopwatch() # Starts automatically on creation
print(f"Stopwatch started. Elapsed: {s.duration:.2f}s")

time.sleep(1.5)
print(f"After 1.5s, Elapsed: {s.duration:.2f}s")

s.stop()
print(f"Stopwatch stopped. Elapsed: {s.duration:.2f}s")

time.sleep(0.5)
print(f"After 0.5s while stopped, Elapsed: {s.duration:.2f}s (should be same)")

s.start() # Resume
time.sleep(0.75)
print(f"After resuming for 0.75s, Total Elapsed: {s.duration:.2f}s")

s.reset() # Reset to 0 and stop
print(f"Stopwatch reset. Elapsed: {s.duration:.2f}s")

view raw JSON →