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())


## 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.1.0.tar.gz (4.6 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.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: time_my_func-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 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.1.0.tar.gz
Algorithm Hash digest
SHA256 61b4b8c866b7266a3a6ed33cd38b48c18f8ddd32ed46565a45c5add464b2d8d2
MD5 2fcb6f9d66782816e3835d4702ba74e7
BLAKE2b-256 34205a0b81de34967e12cb75aca6012af73cdb9256ecc2a0aa897814a02fcb30

See more details on using hashes here.

File details

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

File metadata

  • Download URL: time_my_func-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a6773f28001935ff7c0ba628f87b90fd8f67ac4dd30e5eab6d37e393648ff3a
MD5 c4abd8bd4b1139a9a4e36781e5f6b270
BLAKE2b-256 88ce9a4bc6361ad4aae0ba3a4996899cb571e8364216195cc5abf884f18b513d

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