0

I can profile and visualize my entire application with:

python -m cProfile -o program.prof my_program.py
snakeviz program.prof

Is it possible to write a decorator that will profile only some functions?

In the end this would look like this:

# This is the profiling decorator function.
def profile_function(func):
    def wrapper(*args, **kwargs):
        
        # TODO write profiling information to program.prof
        
        return func(*args, **kwargs)
    return wrapper


@profile_function
def function1():
    for _ in range(0, 1000):
        _**2


@profile_function
def function2():
    for _ in range(0, 100000):
        _**4


# Do not profile function3
def function3():
    return


function1()
function2()
function3()
2
  • 2
    See Profile a section of code Commented Dec 29, 2024 at 10:17
  • @user19077881 thank you! I will have a look at this article. I need to understand how to write the information into one single file program.prof. Commented Dec 29, 2024 at 10:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.