Skip to main content

A custom timer utility for Python

Project description

Todd's Timer

PyPI version License: MIT

A versatile and easy-to-use Python timer utility for measuring code execution time and analyzing performance.

Features

  • Use as a decorator or context manager
  • Nested timing support with proper indentation
  • Detailed logging of start and end times
  • Statistical analysis of execution times across multiple runs
  • Customizable task names with support for dynamic formatting
  • Lightweight and easy to integrate into existing projects

Installation

Install Todd's Timer using pip:

pip install todds_timer

Usage

At the top of your python file, import the Timer class and configure logging. Standard timing messages are logged at the DEBUG level, however the summary table produced by print_average_times() is at the INFO level.

from todds_timer import Timer
import logging

logging.basicConfig(level=logging.DEBUG, format="%(levelname)s %(message)s")

As a Context Manager

with Timer("Calculating sum"):
    result = sum([1,2,3,4])

Output:

DEBUG ----STARTED Calculating sum
DEBUG ----COMPLETED (in 0.000 seconds) Calculating sum

As a Decorator

@Timer("Calculate sum of {0} and {1}")
def add_numbers(a, b):
    return a + b

result = add_numbers(5, 7)

Output:

DEBUG ----STARTED Calculate sum of 5 and 7
DEBUG ----COMPLETED (in 0.000 seconds) Calculate sum of 5 and 7

Nested Timers

with Timer("Outer operation"):
    # Some code here
    with Timer("Inner operation"):
        # More code here
        pass

Output:

DEBUG ----STARTED Outer operation
DEBUG --------STARTED Inner operation
DEBUG --------COMPLETED (in 0.000 seconds) Inner operation
DEBUG ----COMPLETED (in 0.000 seconds) Outer operation

Analyzing Timing Results

# After running some timed operations
Timer.print_average_times(sort="name")

Output:

INFO  Average | Minimum | Maximum | Count | Task name
INFO    0.000 |   0.000 |   0.000 |     1 | Calculate sum of 5 and 7
INFO    0.000 |   0.000 |   0.000 |     1 | Calculating sum
INFO    0.000 |   0.000 |   0.000 |     1 | Inner operation
INFO    0.000 |   0.000 |   0.000 |     1 | Outer operation

Configuration

Todd's Timer uses Python's built-in logging module. To see the timing output, ensure that your logging is configured to show DEBUG level messages:

import logging
logging.basicConfig(level=logging.DEBUG)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

todds_timer-2024.10.16.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

todds_timer-2024.10.16-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file todds_timer-2024.10.16.tar.gz.

File metadata

  • Download URL: todds_timer-2024.10.16.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for todds_timer-2024.10.16.tar.gz
Algorithm Hash digest
SHA256 97f933ae66def97a97f2a749a5082f286ac3d1f26f9f60f080c9259c621e8031
MD5 8e6dca6db48bc0ada954653ce9c205ac
BLAKE2b-256 8bd6e22057dd921d09f3609ab6f87b1de720d3d76376edb76fc8362555d702d6

See more details on using hashes here.

File details

Details for the file todds_timer-2024.10.16-py3-none-any.whl.

File metadata

File hashes

Hashes for todds_timer-2024.10.16-py3-none-any.whl
Algorithm Hash digest
SHA256 a62edc394f0067abf2ef5c6ae26db5f07197e6c66badbd299d8ed34eb050b59a
MD5 8e5b67e20dd9efede99c00f7aa61f73f
BLAKE2b-256 45541123b6e2737cb3b9095587a25ac1fddd8f5d55fdad634221d9d4f2316f6d

See more details on using hashes here.

Supported by

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