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

Aggregated Results & Manual Dump

You can aggregate execution times across multiple calls and functions and print them all at once using dump().

from time_my_func import timeit, dump
import time

@timeit(verbose=False)  # Don't print after each call
def foo():
    time.sleep(0.01)

@timeit(verbose=False)
def bar():
    time.sleep(0.02)

foo()
foo()
bar()

# Print aggregated stats
dump()

## Example Output
[foo] calls=2, avg=10.123 ms, total=20.246 ms
[bar] calls=1, avg=20.456 ms, total=20.456 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

verbose (bool, default=True)
    If True, prints execution time immediately after each call.
    If False, only aggregates for later dump().

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.3.0.tar.gz (6.1 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.3.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: time_my_func-0.3.0.tar.gz
  • Upload date:
  • Size: 6.1 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.3.0.tar.gz
Algorithm Hash digest
SHA256 3654b46b96bce64bb358c3ad5a4892e3f158ad8fd72b4060440a7a698ee4121f
MD5 393cac36a5148509df0366183723af01
BLAKE2b-256 966680dfd3e3520707578bf25a31fc15095d6b065aecfe4243875faba3177aea

See more details on using hashes here.

File details

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

File metadata

  • Download URL: time_my_func-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44ce01e728a916ca714516fc0b4e18c9870313346e015862be9861af5790b19e
MD5 c0106784f39fec5409b0ad5b909f60c4
BLAKE2b-256 394c234c1f0469df9560979c89a01cc65ff85f922c97e37a75822f24daeabcca

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