Skip to content

Code profiling

code_profiling

timer

timer(func)

Wrapper to time and log the function execution.

Parameters:

  • func

    function

Source code in churn_pred/code_profiling.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def timer(func):
    """Wrapper to time and log the function execution.
    Parameters:
        func: function
    """

    @wraps(func)
    def timer_func(*args, **kwargs):
        start_time = time()
        value = func(*args, **kwargs)
        end_time = time()
        logging.info(
            f"Finished {func.__name__} in {(end_time - start_time):.4f} seconds."  # noqa
        )
        return value

    return timer_func

ram_usage

ram_usage(func)

Wrapper to monitor and log RAM usage during the function execution. NOTE: can be applied to function but not to method of a class.

Parameters:

  • func

    function

Source code in churn_pred/code_profiling.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def ram_usage(func):
    """Wrapper to monitor and log RAM usage during the function execution.
    NOTE: can be applied to function but not to method of a class.
    Parameters:
        func: function
    """

    @wraps(func)
    def ram_usage_func(*args, **kwargs):
        ram, value = memory_usage(
            (func, args, *kwargs), interval=1.0, retval=True, max_usage=True
        )
        logging.info(
            f"Finished {func.__name__,}. Max RAM used {(ram / 1000):.4f} GB."
        )  # noqa
        return value

    return ram_usage_func