- Mastering Python Scripting for System Administrators
- Ganesh Sanjiv Naik
- 226字
- 2021-07-02 14:00:33
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
推薦閱讀
- Java多線程編程實(shí)戰(zhàn)指南:設(shè)計(jì)模式篇(第2版)
- Modular Programming with Python
- 數(shù)據(jù)庫(kù)原理及應(yīng)用(Access版)第3版
- 密碼學(xué)原理與Java實(shí)現(xiàn)
- Rust實(shí)戰(zhàn)
- 新手學(xué)Visual C# 2008程序設(shè)計(jì)
- Scratch 3.0少兒編程與邏輯思維訓(xùn)練
- INSTANT Mercurial SCM Essentials How-to
- PLC編程及應(yīng)用實(shí)戰(zhàn)
- Scientific Computing with Scala
- ASP.NET程序設(shè)計(jì)教程
- Learning Concurrent Programming in Scala
- Cybersecurity Attacks:Red Team Strategies
- Scratch趣味編程:陪孩子像搭積木一樣學(xué)編程
- 大學(xué)計(jì)算機(jī)應(yīng)用基礎(chǔ)(Windows 7+Office 2010)(IC3)