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.
In this example, we are trying to calculate the time taken by the program to print a statement.
In this example, we are trying to calculate the time taken by the program to call a function and print the statement.
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.
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:
Here are measuring the time that will take to calculate the following equation.
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}