Skip to content

Ask performance tips #13

Answered by Zhanwei-Liu
Zhanwei-Liu asked this question in Q&A
Jun 26, 2024 · 4 comments · 8 replies
Discussion options

You must be logged in to vote

Thank you!

I modified my timer decorator function by using a Python built-in library called tracemalloc to test peak memory usage due to its convenience. Below is the code I used:

import tracemalloc
def timer(func):
    """
    Decorator to log the start and end of a function, and how long it took to run.

    Args:
        func (function): The function to be decorated.

    Returns:
        function: The decorated function.
    """
    def wrapper(*args, **kwargs):
        # logging.info("Start solving model ...")
        start_time = time.time()
        tracemalloc.start()
        result = func(*args, **kwargs)
        _, peak = tracemalloc.get_traced_memory()
        tracemalloc.stop()…

Replies: 4 comments 8 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@Zhanwei-Liu
Comment options

@metab0t
Comment options

@Zhanwei-Liu
Comment options

@metab0t
Comment options

Comment options

You must be logged in to vote
4 replies
@Zhanwei-Liu
Comment options

@metab0t
Comment options

@Zhanwei-Liu
Comment options

Answer selected by Zhanwei-Liu
@metab0t
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants