Stopwatch for code execution
Project description
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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stwatch-0.1.1.tar.gz.
File metadata
- Download URL: stwatch-0.1.1.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d233b8439cc45610d7ce05f570ce8adab0151a4c1a3ccd445fc7176b7278250c
|
|
| MD5 |
edf4af395ca1fa1c4303cd34b9629777
|
|
| BLAKE2b-256 |
3be9ae073d96f98341d86644e4a2c0c0eb21ab149a1f14d272e0289b6f160865
|
File details
Details for the file stwatch-0.1.1-py3-none-any.whl.
File metadata
- Download URL: stwatch-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1700116e520757b6238fc3f117a8b4347d814824931686a7a4b2bc2641981975
|
|
| MD5 |
1f57ffb7b9b8c1803faaab6f79d48ed7
|
|
| BLAKE2b-256 |
b98b4e8907eb61a339a8ee2febee3697b35395355fceaa390ab357ac24990677
|