Skip to main content

A lightweight Python decorator to measure and print function execution time.

Project description

time_my_func

A lightweight Python decorator for measuring and printing function execution time.
It provides a convenient @timeit() decorator that works on any function (sync or async) and prints execution time in the most appropriate unit, even if the function raises an exception. Supports Python 3.7 and newer (recommended) May work in 3.6 if perf_counter_ns() is adjusted to have a fallback.


Installation

# Install from PyPI
pip install time_my_func

# Or install from source
git clone https://github.com/DeathlyDestiny/function_timer
cd time_my_func
pip install .

Usage Synchronous functions

from time_my_func import timeit
import time

@timeit()
def fast_function():
    sum(range(100))

@timeit(decimals=5, unit="ms")
def slow_function():
    time.sleep(0.123)

fast_function()
slow_function()

Usage Asynchronous functions

import asyncio
from time_my_func import timeit

@timeit(unit="ms", decimals=4)
async def async_task():
    await asyncio.sleep(0.05)

asyncio.run(async_task())

Handling exceptions

import asyncio
from time_my_func import timeit

@timeit(unit="ms", decimals=4)
async def async_task():
    await asyncio.sleep(0.05)

asyncio.run(async_task())

Global Toggle

from time_my_func import set_enabled

set_enabled(False)  # Disable all @timeit() decorators
set_enabled(True)   # Re-enable

## Example Output
[fast_function] Execution time: 12.345 µs
[slow_function] Execution time: 123.45678 ms
[async_task] Execution time: 50.1234 ms
[failing] Execution time: 0.001 ms

Options

The decorator accepts two optional arguments:

decimals (int, default = 3)
Number of decimal places to display.

unit (str, default = "auto")
Force the time unit. Options:

"auto"  automatically choose the best unit based on elapsed time
"ns"  nanoseconds
"us" or "µs"  microseconds
"ms"  milliseconds
"s"  seconds
"m"  minutes

The decorator works for both synchronous and asynchronous functions, and will always print execution time, even if the function raises an exception.

Contributing

Contributions are welcome!

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

time_my_func-0.2.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

time_my_func-0.2.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file time_my_func-0.2.1.tar.gz.

File metadata

  • Download URL: time_my_func-0.2.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for time_my_func-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1a17d48e893c8894111a00d5d0a3ce3b43d0cfe2e51773a4553d4b123a1aafac
MD5 c1663cee2b333803302452571bccdce7
BLAKE2b-256 02c94f044db7294ab0e181aefe7582ad77703468e3017556fef00903d3324b47

See more details on using hashes here.

File details

Details for the file time_my_func-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: time_my_func-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for time_my_func-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d23ac7f90989ef7774ee94162b7bee60969a28c034b36a47a436ee279b31bf38
MD5 7cf1f0b77f915bcc628d73f11eb0ce23
BLAKE2b-256 17c890919c72181e3c46af6246c61107bac11724d86f49d5488d97441da0a2ac

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