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
# importing cProfile 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
# importing cProfile 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
# importing library import cProfile import pstats import io from pstats import SortKey # Creating profile object ob = cProfile.Profile() ob.enable() # As you increase the power time will increase # as per your machine efficiency. 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}
Profiling in Python
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.
Contact Us