stouputils.decorators.timing module#

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]][source]#
measure_time(
func: None = 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]]]
measure_time(
func: Callable[..., T],
*,
printer: Callable[[...], None] = progress,
message: str = '',
perf_counter: bool = True,
is_generator: Literal[False] = False,
) → Callable[..., T]
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]]

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

Parameters:
  • func – Function to decorate

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

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

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

  • is_generator – 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.

Examples

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