stwatch
A lightweight Python stopwatch library for timing code execution with precision. Features include lap timing, function execution timing, and context manager support.
Features
- Simple start/stop timing
- Lap timing with named laps
- Function execution timing
- Context manager support
- Elapsed time tracking
- Lap time analysis
- String representations for debugging
Installation
pip install stwatch
Usage
Basic Timing
from stwatch import Stopwatch
# Simple start/stop
sw = Stopwatch()
sw.start()
# ... your code here ...
elapsed = sw.stop()
print(f"Operation took {elapsed:.2f} seconds")
# Auto-start initialization
sw = Stopwatch(start=True)
# ... your code here ...
elapsed = sw.stop()
# Using context manager
with Stopwatch() as sw:
# ... your code here ...
print(f"Current time: {sw.elapsed_time():.2f}")
Lap Timing
sw = Stopwatch(start=True)
# Record named laps
lap_time, total_time = sw.lap("database_query")
lap_time, total_time = sw.lap("data_processing")
# Auto-named laps
lap_time, total_time = sw.lap() # Named "lap 1"
lap_time, total_time = sw.lap() # Named "lap 2"
# Get lap information
# By name
db_lap_time, db_total = sw.get_lap(name="database_query")
# By index
first_lap_time, first_total = sw.get_lap(index=0)
# Get time since last lap
time_since_last = sw.elapsed_since_lap()
# Get time since specific lap
time_since_db = sw.elapsed_since_lap("database_query")
Function Timing
from stwatch import Stopwatch
def expensive_operation(x, y, multiplier=1):
# ... some time-consuming code ...
return (x + y) * multiplier
sw = Stopwatch()
# Time function with arguments
time_taken, result = sw.time_function(
expensive_operation,
2,
3,
multiplier=2
)
print(f"Function took {time_taken:.2f} seconds and returned {result}")
Advanced Usage
# Nested timing
with Stopwatch() as outer:
# ... some code ...
with Stopwatch() as inner:
# ... nested operation ...
inner_time = inner.elapsed_time()
outer_time = outer.elapsed_time()
# String representation for debugging
sw = Stopwatch(start=True)
sw.lap("first")
print(sw) # Outputs: Stopwatch(running=True, elapsed_time=0.001, elapsed_since_lap=0.001)
API Reference
Constructor
Stopwatch(start=False): Create a new stopwatch instancestart: Whether to start timing immediately
Methods
start(): Start the stopwatchstop() -> float: Stop the stopwatch and return total timelap(name=None) -> tuple[float, float]: Record a lap, returns (lap_time, total_time)get_lap(index=None, name=None) -> tuple[float, float]: Get lap timing by index or nameelapsed_time() -> float: Get current total elapsed timeelapsed_since_lap(name=None) -> float: Get time elapsed since specific laptime_function(func, *args, **kwargs) -> tuple[float, any]: Time a function execution
Properties
is_running -> bool: Check if stopwatch is currently runninglaps -> list[tuple[str, float, float]]: Access recorded laps
Error Handling
The library raises appropriate exceptions for invalid operations:
RuntimeError: When stopping/lapping before startingValueError: 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 stwatch 0.1.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 | |
|---|---|---|---|
| stwatch-0.1.1.tar.gz | 6.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| stwatch-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.8 kB
Release files / stwatch-0.1.1.tar.gz
| Download URL | stwatch-0.1.1.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d233b8439cc45610d7ce05f570ce8adab0151a4c1a3ccd445fc7176b7278250c
|
|
BLAKE2b-256 checksum How to use checksums |
3be9ae073d96f98341d86644e4a2c0c0eb21ab149a1f14d272e0289b6f160865
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / stwatch-0.1.1-py3-none-any.whl
| Download URL | stwatch-0.1.1-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1700116e520757b6238fc3f117a8b4347d814824931686a7a4b2bc2641981975
|
|
BLAKE2b-256 checksum How to use checksums |
b98b4e8907eb61a339a8ee2febee3697b35395355fceaa390ab357ac24990677
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|