Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3595478
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:51:27+00:00 2026-05-18T19:51:27+00:00

I want to profile my Python code. I am well-aware of cProfile , and

  • 0

I want to profile my Python code. I am well-aware of cProfile, and I use it, but it’s too low-level. (For example, there isn’t even a straightforward way to catch the return value from the function you’re profiling.)

One of the things I would like to do: I want to take a function in my program and set it to be profiled on the fly while running the program.

For example, let’s say I have a function heavy_func in my program. I want to start the program and have the heavy_func function not profile itself. But sometime during the runtime of my program, I want to change heavy_func to profile itself while it’s running. (If you’re wondering how I can manipulate stuff while the program is running: I can do it either from the debug probe or from the shell that’s integrated into my GUI app.)

Is there a module already written which does stuff like this? I can write it myself but I just wanted to ask before so I won’t be reinventing the wheel.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:51:28+00:00Added an answer on May 18, 2026 at 7:51 pm

    I wrote my own module for it. I called it cute_profile. Here is the code. Here are the tests.

    Here is the blog post explaining how to use it.

    It’s part of GarlicSim, so if you want to use it you can install garlicsim and do from garlicsim.general_misc import cute_profile.

    If you want to use it on Python 3 code, just install the Python 3 fork of garlicsim.

    Here’s an outdated excerpt from the code:

    import functools
    
    from garlicsim.general_misc import decorator_tools
    
    from . import base_profile
    
    
    def profile_ready(condition=None, off_after=True, sort=2):
        '''
        Decorator for setting a function to be ready for profiling.
    
        For example:
    
            @profile_ready()
            def f(x, y):
                do_something_long_and_complicated()
    
        The advantages of this over regular `cProfile` are:
    
         1. It doesn't interfere with the function's return value.
    
         2. You can set the function to be profiled *when* you want, on the fly.
    
        How can you set the function to be profiled? There are a few ways:
    
        You can set `f.profiling_on=True` for the function to be profiled on the
        next call. It will only be profiled once, unless you set
        `f.off_after=False`, and then it will be profiled every time until you set
        `f.profiling_on=False`.
    
        You can also set `f.condition`. You set it to a condition function taking
        as arguments the decorated function and any arguments (positional and
        keyword) that were given to the decorated function. If the condition
        function returns `True`, profiling will be on for this function call,
        `f.condition` will be reset to `None` afterwards, and profiling will be
        turned off afterwards as well. (Unless, again, `f.off_after` is set to
        `False`.)
    
        `sort` is an `int` specifying which column the results will be sorted by.
        '''
    
    
        def decorator(function):
    
            def inner(function_, *args, **kwargs):
    
                if decorated_function.condition is not None:
    
                    if decorated_function.condition is True or \
                       decorated_function.condition(
                           decorated_function.original_function,
                           *args,
                           **kwargs
                           ):
    
                        decorated_function.profiling_on = True
    
                if decorated_function.profiling_on:
    
                    if decorated_function.off_after:
                        decorated_function.profiling_on = False
                        decorated_function.condition = None
    
                    # This line puts it in locals, weird:
                    decorated_function.original_function
    
                    base_profile.runctx(
                        'result = '
                        'decorated_function.original_function(*args, **kwargs)',
                        globals(), locals(), sort=decorated_function.sort
                    )                
                    return locals()['result']
    
                else: # decorated_function.profiling_on is False
    
                    return decorated_function.original_function(*args, **kwargs)
    
            decorated_function = decorator_tools.decorator(inner, function)
    
            decorated_function.original_function = function
            decorated_function.profiling_on = None
            decorated_function.condition = condition
            decorated_function.off_after = off_after
            decorated_function.sort = sort
    
            return decorated_function
    
        return decorator
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.