pyfunctools.decorators.timing_decorator module

no-index:

pyfunctools.decorators.timing_decorator.timing_decorator(log_func: Callable[[str, float, float], Any] = None)[source]

A decorator to measure the execution time of a function.

This decorator measures the time taken by the decorated function to execute and logs the start time, end time, and duration. It can use a custom logging function if provided; otherwise, it defaults to printing.

Parameters:

log_func (t.Callable[[str, float, float], t.Any], optional) – A custom logging function that accepts three arguments: the function name, start time, and end time. If not provided, it defaults to printing the information.

Returns:

A function that measures the execution time of the original function.

Return type:

callable

Example

>>> def custom_logger(func_name, start_time, end_time):
...     print(f'{func_name} started at * and ended at *, taking * seconds')
>>>
>>> @timing_decorator(log_func=custom_logger)
... def example_function(x):
...     time.sleep(2)
...     return x * 2
>>>
>>> print(example_function(5))
example_function started at * and ended at *, taking * seconds
10