stouputils.decorators.measure_time module#

measure_time(
func: Callable[..., T],
*,
printer: Callable[[...], None] = progress,
message: str = '',
perf_counter: bool = True,
is_generator: Literal[False] = False,
) Callable[..., T][source]#
measure_time(
func: None = None,
*,
printer: Callable[[...], None] = progress,
message: str = '',
perf_counter: bool = True,
is_generator: Literal[False] = False,
) Callable[[Callable[..., T]], Callable[..., T]]
measure_time(
func: Callable[..., Generator[T, None, None]],
*,
printer: Callable[[...], None] = progress,
message: str = '',
perf_counter: bool = True,
is_generator: Literal[True],
) Callable[..., Generator[T, None, None]]
measure_time(
func: None,
*,
printer: Callable[[...], None] = progress,
message: str = '',
perf_counter: bool = True,
is_generator: Literal[True],
) Callable[[Callable[..., Generator[T, None, None]]], Callable[..., Generator[T, None, None]]]

Decorator that will measure the execution time of a function and print it with the given print function

Parameters:
  • func (Callable[..., Any] | None) – Function to decorate

  • printer (Callable) – Function to use to print the execution time (e.g. debug(), info(), warning(), error(), etc.)

  • message (str) – Message to display with the execution time (e.g. “Execution time of Something”), defaults to “Execution time of {func.__name__}”

  • perf_counter (bool) – Whether to use time.perf_counter_ns or time.time_ns defaults to True (use time.perf_counter_ns)

  • is_generator (bool) – Whether the function is a generator or not (default: False) When True, the decorator will yield from the function instead of returning it.

Returns:

Decorator to measure the time of the function.

Return type:

Callable

Examples

> @measure_time(printer=info)
> def test():
>     pass
> test()  # [INFO HH:MM:SS] Execution time of test: 0.000ms (400ns)