w3resource

How do you profile specific functions or modules using cProfile?

Profiling specific functions and modules with cProfile

Profiling specific functions or modules using 'cProfile' is achieved by selecting only the parts of the code you want to profile. To profile specific functions or modules, use the 'cProfile.run()' function or the 'cProfile.Profile()' class. Here's how to do it:

Profiling Specific Functions:

Code (test_func.py):

# test_func.py
def add_func_1():
    tot = 0
    for x in range(800000):
        tot = tot + x

def add_func_2():
    tot = 0
    for x in range(80):
        tot = tot + x 

Code (main.py):

# main.py
import cProfile
import test_func
# Profile only the add_func_1
cProfile.run('test_func.add_func_1()')
# Profile only the add_func_2
cProfile.run('test_func.add_func_2()')
# Profile multiple functions
cProfile.run('test_func.add_func_1(); test_func.add_func_2()')

Run main.py in Spyder IDE

Reloaded modules: test_func
         4 function calls in 0.030 seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.030    0.030 <string>:1(<module>)
        1    0.030    0.030    0.030    0.030 test_func.py:2(add_func_1)
        1    0.000    0.000    0.030    0.030 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
         4 function calls in 0.000 seconds

Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 test_func.py:7(add_func_2)
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
         5 function calls in 0.030 seconds

Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.030    0.030 <string>:1(<module>)
        1    0.030    0.030    0.030    0.030 test_func.py:2(add_func_1)
        1    0.000    0.000    0.000    0.000 test_func.py:7(add_func_2)
        1    0.000    0.000    0.030    0.030 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

In this example, we have two functions add_func_1 () and add_func_2() defined in main.py. We use cProfile.run() to profile the execution time of these.

Profiling Specific Modules:

Code (test_func.py):

# test_func.py
def add_func_1():
    tot = 0
    for x in range(800000):
        tot = tot + x
def add_func_2():
    tot = 0
    for x in range(80):
        tot = tot + x 

Code (main.py):

# main.py
import cProfile
import test_func
# Create a cProfile instance
profiler = cProfile.Profile()
# Profile a specific module
profiler.run('test_func.add_func_1()')
# Profile multiple functions in a module
profiler.run('test_func.add_func_1(); test_func.add_func_2()')
# Get the profiling results
profiler.print_stats()

Run main.py in Spyder IDE

Reloaded modules: test_func
         8 function calls in 0.072 seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.030    0.030 :1()
        2    0.072    0.036    0.072    0.036 test_func.py:2(add_func_1)
        1    0.000    0.000    0.000    0.000 test_func.py:7(add_func_2)
        2    0.000    0.000    0.072    0.036 {built-in method builtins.exec}
        2    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

In this example, we create a cProfile.Profile() instance, and then we use its run() method to profile the execution time of specific functions or modules.

Selectively profiling specific functions or modules will help you focus your performance analysis on the parts of code that need to be optimized. By doing this, you can focus your efforts on the areas that have the greatest impact on the overall performance of your Python application.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-interview/how-do-you-profile-specific-functions-or-modules-using-cprofile.php