官术网_书友最值得收藏!

The cProfile module

As discussed previously, profiling means measuring the execution time of a program. We are going to use the cProfile Python module for profiling a program.

Now, we will write a cprof_example.py script and write the following code in it:

mul_value = 0
def mul_numbers( num1, num2 ):
mul_value = num1 * num2;
print ("Local Value: ", mul_value)
return mul_value
mul_numbers( 58, 77 )
print ("Global Value: ", mul_value)

Run the program and you will see the output as follows:

student@ubuntu:~$ python3 -m cProfile cprof_example.py
Local Value: 4466
Global Value: 0
6 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 cprof_example.py:1(<module>)
1 0.000 0.000 0.000 0.000 cprof_example.py:2(mul_numbers)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
2 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}

So, using cProfile, all functions that are called will get printed with the time spent on each function. Now, we will see what these column headings mean:

  • ncalls: Number of calls
  • tottime: Total time spent in the given function
  • percall: Quotient of tottime divided by ncalls
  • cumtime: Cumulative time spent in this and all subfunctions
  • percall: Quotient of cumtime divided by primitive calls
  • filename:lineno(function): Provides the respective data of each function
主站蜘蛛池模板: 利津县| 潜山县| 武乡县| 上高县| 泰兴市| 黎川县| 晋城| 通海县| 龙川县| 平乐县| 五原县| 贡觉县| 剑阁县| 陈巴尔虎旗| 襄城县| 江阴市| 集贤县| 宣恩县| 金湖县| 葫芦岛市| 正镶白旗| 蕲春县| 陈巴尔虎旗| 布拖县| 石城县| 垫江县| 邳州市| 益阳市| 西贡区| 娱乐| 河源市| 林西县| 河池市| 砚山县| 西充县| 阿拉尔市| 莲花县| 衡南县| 竹山县| 巫山县| 洛川县|