stw ⏱️
A lightweight Python stopwatch library for timing code execution with precision. Whether you need to benchmark performance, time specific operations, or track code execution, stw provides a simple yet powerful interface.
Key Features
- 🔄 Simple start/stop timing
- 🏁 Lap timing with named checkpoints
- ⚡ Function execution benchmarking
- 📊 Visual lap time diagrams
- 🧩 Context manager support (
withstatement) - 📈 Elapsed time tracking
- 🔍 Human-readable time formatting
Installation
pip install stw
Quick Start
from stw import Stopwatch
# Basic timing
sw = Stopwatch(name="quickstart")
sw.lap("begin") # records a lap named "begin"
# ... your code here ...
sw.lap("end")
print(f"Total time: {sw.elapsed_total()}s")
Usage Examples
Basic Timing
from stw import Stopwatch
import time
# Method 1: Manual start/stop
sw = Stopwatch(name="example1")
sw.lap("start")
time.sleep(1.5) # simulate work
total_time, lap_time = sw.lap("end")
print(f"Operation took {lap_time[0]:.2f} seconds") # lap_time
# Method 2: Using context manager (automatically records start/end laps)
with Stopwatch(name="example2", verbose=True) as sw:
time.sleep(1.2) # simulate work
# The elapsed time is automatically printed when exiting the context
Timing Multiple Operations
from stw import Stopwatch
import time
# Create a stopwatch with verbose output
sw = Stopwatch(name="database-ops", verbose=True)
# Record start lap
sw.lap("begin")
# First operation
time.sleep(0.8) # simulate database query
sw.lap("query")
# Prints: [database-ops] query: 0.80s | total: 0.80s
# Second operation
time.sleep(0.5) # simulate data processing
sw.lap("processing")
# Prints: [database-ops] processing: 0.50s | total: 1.30s
# Third operation
time.sleep(0.3) # simulate rendering
sw.lap("render")
# Prints: [database-ops] render: 0.30s | total: 1.60s
# Get time info for a specific operation
timestamp, total_time, lap_time = sw.get_lap(name="query")
print(f"Query operation took {lap_time:.2f}s")
# Print a visual diagram of all operations
sw.print_diagram()
Function Benchmarking
from stw import Stopwatch, stopwatch
import time
# Method 1: Using the time_function method
def expensive_calculation(n, factor=2):
time.sleep(n * 0.1) # simulate work
return n * factor
sw = Stopwatch()
# Time any function with any arguments
time_taken, result = sw.time_function(
expensive_calculation,
5, # positional arg
factor=3 # keyword arg
)
print(f"Calculation took {time_taken:.2f}s and returned {result}")
# Method 2: Using the decorator
@stopwatch
def process_data(items, multiplier=1):
time.sleep(0.2) # simulate work
return sum(items) * multiplier
# Function execution is automatically timed
result = process_data([1, 2, 3, 4], multiplier=2)
# Prints timing information automatically
Nested Timing
from stw import Stopwatch
import time
# Timing nested operations
with Stopwatch(name="outer", verbose=True) as outer:
time.sleep(0.5) # some initial work
with Stopwatch(name="inner", verbose=True) as inner:
time.sleep(0.8) # nested operation
time.sleep(0.3) # more outer work
# Access timing information from both stopwatches
print(f"Inner operation: {inner.elapsed_total():.2f}s")
print(f"Total with overhead: {outer.elapsed_total():.2f}s")
Advanced Features
from stw import Stopwatch
import time
sw = Stopwatch(name="advanced")
# Record some operations
sw.lap("first")
time.sleep(0.5)
sw.lap("second")
time.sleep(0.8)
sw.lap("third")
# Get time since a specific lap
time_since_second = sw.elapsed_since_lap("second")
print(f"Time since 'second' lap: {time_since_second:.2f}s")
# Get time until a specific lap
time_until_second = sw.elapsed_total("second")
print(f"Time until 'second' lap: {time_until_second:.2f}s")
# Access all recorded laps
for lap_name, timestamp, total_time, lap_time in sw.laps:
print(f"Lap '{lap_name}': {lap_time:.2f}s (Total: {total_time:.2f}s)")
# Visual representation
print(sw) # Shows current state with elapsed time
sw.print_diagram() # Prints a visual diagram of all laps
API Reference
Constructor
Stopwatch(name="stw", verbose=False): Create a new stopwatchname: Name for this stopwatch (useful for identifying in output)verbose: When True, automatically prints lap timings
Methods
-
lap(lap_name=None) -> tuple[float, float]: Record a lap, returns (total_time, lap_time) -
get_lap(index=None, name=None) -> tuple[float, float, float]: Get (timestamp, total_time, lap_time) by index or name -
elapsed_until_lap(name=None) -> float: Get time until the last lap or a specific lap -
elapsed_since_lap(name=None) -> float: Get time since the last lap or a specific lap -
time_function(func, *args, **kwargs) -> tuple[float, any]: Time a function execution -
print_diagram(): Print a visual representation of lap times
Properties
laps -> list[tuple[str, float, float, float]]: Access all recorded laps- Each lap is a tuple of (lap_name, timestamp, total_time, lap_time)
Decorators
@stopwatch: Decorator to automatically time a function
Error Handling
The library raises appropriate exceptions for invalid operations:
ValueError: When accessing invalid laps or providing invalid arguments
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License
Metadata
Release files for stw 0.3.0
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 |
|---|---|---|---|---|
| stw-0.3.0.tar.gz | 10.5 kB | Without provenance | Details for stw-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| stw-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for stw-0.3.0-py3-none-any.whl |
Total release size: 17.6 kB
Release files / stw-0.3.0.tar.gz
| Download URL | stw-0.3.0.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3d08f7a13e49e0c6715da21097ca75b95fc0ab4f068eb4ac088c60620aa0e8ca
|
|
BLAKE2b-256 checksum How to use checksums |
466e59e2e8a1df98a0b0a92c84bd3f972414e4fdc07aac8befe89530d71d128e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / stw-0.3.0-py3-none-any.whl
| Download URL | stw-0.3.0-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bce29fd1cfb40bdc03017c3c5105da5ebfaf0d9c2ee9c640a68203cf93da4159
|
|
BLAKE2b-256 checksum How to use checksums |
c1e0b66cfae06176991b32d4737b4cb32fdfb51bb7691725234812a255e9cc5e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Security for stw 0.3.0
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.