Skip to main content

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 instance
    • start: Whether to start timing immediately

Methods

  • start(): Start the stopwatch
  • stop() -> float: Stop the stopwatch and return total time
  • lap(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 name
  • elapsed_time() -> float: Get current total elapsed time
  • elapsed_since_lap(name=None) -> float: Get time elapsed since specific lap
  • time_function(func, *args, **kwargs) -> tuple[float, any]: Time a function execution

Properties

  • is_running -> bool: Check if stopwatch is currently running
  • laps -> list[tuple[str, float, float]]: Access recorded laps

Error Handling

The library raises appropriate exceptions for invalid operations:

  • RuntimeError: When stopping/lapping before starting
  • ValueError: 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stwatch-0.1.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stwatch-0.1.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

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

Hashes for stwatch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d233b8439cc45610d7ce05f570ce8adab0151a4c1a3ccd445fc7176b7278250c
MD5 edf4af395ca1fa1c4303cd34b9629777
BLAKE2b-256 3be9ae073d96f98341d86644e4a2c0c0eb21ab149a1f14d272e0289b6f160865

See more details on using hashes here.

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

Hashes for stwatch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1700116e520757b6238fc3f117a8b4347d814824931686a7a4b2bc2641981975
MD5 1f57ffb7b9b8c1803faaab6f79d48ed7
BLAKE2b-256 b98b4e8907eb61a339a8ee2febee3697b35395355fceaa390ab357ac24990677

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page