Profiling in Python
Last Updated :
06 Sep, 2022
Python provides many excellent modules to measure the statistics of a program. This makes us know where the program is spending too much time and what to do in order to optimize it. It is better to optimize the code in order to increase the efficiency of a program. So, perform some standard tests to ensure optimization and we can improve the program in order to increase efficiency. In this article, we will cover How do we profile a Python script to know where the program is spending too much time and what to do in order to optimize it.
Method 1: Python time module
Time in Python is easy to implement and it can be used anywhere in a program to measure the execution time. By using timers we can get the exact time and we can improve the program where it takes too long. The time module provides the methods in order to profile a program.
Example 1:
In this example, we are trying to calculate the time taken by the program to print a statement.
Python3
import time
start = time.time()
print ( "Time Consumed" )
print ( "% s seconds" % (time.time() - start))
|
Output:
Time Consumed
0.01517796516418457 seconds
Example 2:
In this example, we are trying to calculate the time taken by the program to call a function and print the statement.
Python3
import time
def gfg():
start = time.time()
print ( "Time consumed" )
end = time.time()
print ( "gfg() function takes" , end - start, "seconds" )
gfg()
|
Output:
Time consumed
gfg() function takes 0.015180110931396484 seconds
Method 2: Python line_profiler
Python provides a built-in module to measure execution time and the module name is LineProfiler.It gives a detailed report on the time consumed by a program.
Python3
from line_profiler import LineProfiler
def geek(rk):
print (rk)
rk = "geeks"
profile = LineProfiler(geek(rk))
profile.print_stats()
|
Output:
Timer unit: 4.27198e-10 s
Method 3: Python cProfile
Python includes a built-in module called cProfile which is used to measure the execution time of a program. The cProfiler module provides all information about how long the program is executing and how many times the function gets called in a program. The Python cprofile example:
Example 1:
Here are measuring the time that will take to calculate the following equation.
Python3
import cProfile
cProfile.run( "10 + 10" )
|
Output:
3 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 :1()
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}
Example 2:
The cProfile measures the statistics about any function.
Python3
import cProfile
def f():
print ( "hello" )
cProfile.run( 'f()' )
|
Output:
hello
5 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 3233da5f950795af777f4b63136f7efd.py:5(f)
1 0.000 0.000 0.000 0.000 :1()
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Example 3:
If more exact profiling control is required then Profile class is normally used over the cProfile.run() function provides.
Python3
import cProfile
import pstats
import io
from pstats import SortKey
ob = cProfile.Profile()
ob.enable()
num = 18 * * 200000
ob.disable()
sec = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(ob, stream = sec).sort_stats(sortby)
ps.print_stats()
print (sec.getvalue())
|
Output:
45 function calls in 0.044 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
2 0.000 0.000 0.044 0.022 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3396(run_code)
2 0.000 0.000 0.044 0.022 {built-in method builtins.exec}
1 0.044 0.044 0.044 0.044 <ipython-input-1-f7443c5d9495>:11(<module>)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\codeop.py:142(__call__)
2 0.000 0.000 0.000 0.000 {built-in method builtins.compile}
1 0.000 0.000 0.000 0.000 <ipython-input-1-f7443c5d9495>:13(<module>)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\contextlib.py:238(helper)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\contextlib.py:82(__init__)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\traitlets\traitlets.py:564(__get__)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\contextlib.py:117(__exit__)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\contextlib.py:108(__enter__)
4 0.000 0.000 0.000 0.000 {built-in method builtins.next}
4 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\compilerop.py:166(extra_flags)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\hooks.py:103(__call__)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\traitlets\traitlets.py:533(get)
4 0.000 0.000 0.000 0.000 {built-in method builtins.getattr}
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3334(compare)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\utils\ipstruct.py:125(__getattr__)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:1278(user_global_ns)
2 0.000 0.000 0.000 0.000 C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\hooks.py:168(pre_run_code_hook)
1 0.000 0.000 0.000 0.000 {method ‘disable’ of ‘_lsprof.Profiler’ objects}
Share your thoughts in the comments
Please Login to comment...