stouputils.ctx.measure_time module#
- class MeasureTime(print_func: ~collections.abc.Callable[[...], None] = <function debug>, message: str = 'Execution time', perf_counter: bool = True)[source]#
Bases:
AbstractBothContextManager[MeasureTime]Context manager to measure execution time.
This context manager measures the execution time of the code block it wraps and prints the result using a specified print function.
- Parameters:
print_func (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. Defaults to “Execution time”.
perf_counter (bool) – Whether to use time.perf_counter_ns or time.time_ns. Defaults to True.
Examples
> import time > import stouputils as stp > with stp.MeasureTime(stp.info, message="My operation"): ... time.sleep(0.5) > # [INFO HH:MM:SS] My operation: 500.123ms (500123456ns) > with stp.MeasureTime(): # Uses debug by default ... time.sleep(0.1) > # [DEBUG HH:MM:SS] Execution time: 100.456ms (100456789ns)
- print_func: Callable[[...], None]#
Function to use for printing the execution time
- message: str#
Message to display with the execution time
- perf_counter: bool#
Whether to use time.perf_counter_ns or time.time_ns
- ns: Callable[[], int]#
Time function to use
- _abc_impl = <_abc._abc_data object>#
- start_ns: int#
Start time in nanoseconds