Skip to main content

A versatile timing decorator

Project description

Timeit Decorator

License: MIT

Overview

timeit_decorator is a Python package providing a versatile decorator for timing the execution of functions. It supports executing functions multiple times, in parallel, and can use either threading or multiprocessing depending on the nature of the task.

Logging Instead of Printing

The timeit_decorator outputs timing information exclusively through Python's logging framework, rather than printing directly to the console. This approach offers more flexibility and control, allowing users to customize the output format, level, and destination. It integrates seamlessly with your application's logging configuration.

Installation

To install timeit_decorator, run the following command:

pip install timeit-decorator

Usage

Basic Usage

Here's a simple example of how to use the timeit decorator, including how it integrates with Python's logging system:

import logging
from timeit_decorator import timeit

# Configure logging
logging.basicConfig(level=logging.INFO)
# Default parameters :
# - runs: int = 1,
# - workers: int = 1,
# - log_level: int = logging.INFO,
# - use_multiprocessing: bool = False
@timeit()
def sample_function():
    # Function implementation

In this example, the timeit decorator will log the execution time of sample_function using Python's logging framework.

Using Multiprocessing

For CPU-bound tasks, you can enable multiprocessing:

import logging
from timeit_decorator import timeit

# Configure logging
logging.basicConfig(level=logging.DEBUG)

@timeit(runs=10, workers=4, log_level=logging.DEBUG, use_multiprocessing=True)
def cpu_intensive_function():
    # CPU-bound function implementation

Using Threading (Default)

For I/O-bound tasks, the default threading is more efficient:

import logging
from timeit_decorator import timeit

# Configure logging
logging.basicConfig(level=logging.INFO)

@timeit(runs=5, workers=2, log_level=logging.INFO)
def io_bound_function():
    # I/O-bound function implementation

Features

  • Multiple Runs: Execute the function multiple times for more accurate timing.
  • Concurrency: Run tasks in parallel using either threading or multiprocessing.
  • Flexible: Suitable for both CPU-bound and I/O-bound tasks.
  • Customizable: Control the number of runs and workers, and choose between threading and multiprocessing.

Requirements

timeit_decorator requires Python 3.x.

Contributing

Contributions to timeit_decorator are welcome! Please read our contributing guidelines for more details.

License

timeit_decorator is released under the 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

timeit_decorator-1.0.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

timeit_decorator-1.0.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file timeit_decorator-1.0.2.tar.gz.

File metadata

  • Download URL: timeit_decorator-1.0.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for timeit_decorator-1.0.2.tar.gz
Algorithm Hash digest
SHA256 702c05bebf0fcfe501828b9d4e83a847003dae72b5c71aed6f4c053b86781534
MD5 e515de8f3b970289edce44caf8a2bb01
BLAKE2b-256 199fdc1a5544cd2f7a34907b548cd9a27c2ec4d7f71afc0400a9882133eb8dee

See more details on using hashes here.

File details

Details for the file timeit_decorator-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for timeit_decorator-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 afb9295f0eb1560d4dbb279c7ff02c07611ef1348f85df2926d16f7015dbdc05
MD5 990522da1cdfa1c95e8854a12b13d7ac
BLAKE2b-256 b007b5da221f8aa9886e256df33a4cab7b7c5d09e0d7f5b9682ab140c1927986

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