Python module: profile,pstats

Source: Internet
Author: User

Profile and Pstats are the parser for Python code that can objectively look at the quality of the code and the resources used. There is a lot of help in debugging the program.


1. Using profile to analyze Python code

[Email protected] tmp]# vim profile12.py

#!/bin/env python
#!-*-Coding:utf-8-*-
Import profile

Def one (): #定义一个one函数

Sum=0
For I in range (10000):
Sum+=i
return sum

Def ():
Sum=0
For I in range (100000):
Sum+=i
return sum

def there ():
Sum=0
For I in range (100000):
Sum+=i
return sum

If __name__== "__main__":
Profile.run ("One ()", "result") #将结果保存到result文件中

Profile.run ("both ()")
Profile.run ("there ()")
[email protected] tmp]# python profile12.py
5 function calls in 0.010 CPU seconds
Ordered By:standard Name
Ncalls tottime percall cumtime percall filename:lineno (function)
1 0.003 0.003 0.003 0.003:0 (range)
1 0.000 0.000 0.000 0.000:0 (setprofile)
1 0.000 0.000 0.010 0.010 <string>:1 (<module>)
1 0.007 0.007 0.010 0.010 profile12.py:12 (both)
0 0.000 0.000 profile:0 (Profiler)
1 0.000 0.000 0.010 0.010 profile:0 (both ())
Ncalls: Number of function calls

Tottime: The total elapsed time of the function, eliminating the run time of the call child function in the function

Percall: (first percall) equals tottime/ncalls

Cumtime: The time at which the function and all its child functions are called to run, that is, when the function starts calling to the return time

Percall: (the second percall) is the average time the function runs once, equal to Cumtime/ncalls

Filename:lineno (function): Specific information for each function call


5 function calls in 0.008 CPU seconds
Ordered By:standard Name
Ncalls tottime percall cumtime percall filename:lineno (function)
1 0.001 0.001 0.001 0.001:0 (range)
1 0.000 0.000 0.000 0.000:0 (setprofile)
1 0.000 0.000 0.008 0.008 <string>:1 (<module>)
1 0.007 0.007 0.008 0.008 profile12.py:18 (there)
0 0.000 0.000 profile:0 (Profiler)
1 0.000 0.000 0.008 0.008 profile:0 (There ())

Thu May 5 17:30:09 result
5 function calls in 0.001 CPU seconds
Ordered By:standard Name
Ncalls tottime percall cumtime percall filename:lineno (function)
1 0.000 0.000 0.000 0.000:0 (range)
1 0.000 0.000 0.000 0.000:0 (setprofile)
1 0.000 0.000 0.001 0.001 <string>:1 (<module>)
1 0.001 0.001 0.001 0.001 profile12.py:6 (one)
1 0.000 0.000 0.001 0.001 profile:0 (one ())
0 0.000 0.000 profile:0 (Profiler)
[Email protected] tmp]#


2. Analyzing Python code with pstats

[Email protected] tmp]# vim profile12.py

#!/bin/env python
#!-*-Coding:utf-8-*-

Import Profile,pstats

Def one ():
Sum=0
For I in range (10000):
Sum+=i
return sum

If __name__== "__main__":
Profile.run ("One ()", "result") #将结果保存到result文件中

P=pstats. Stats ("result") #创建一上pstats变量
P.strip_dirs (). Sort_stats ( -1). Print_stats () #strip_dirs: 从所有模块名中去掉无关的路径信息

P.strip_dirs (). Sort_stats ("name"). Print_stats ()#sort_stats():把打印信息按照标准的module/name/line字符串进行排序

P.strip_dirs (). Sort_stats ("cumulative"). Print_stats (3)#print_stats():打印出所有分析信息

[email protected] tmp]# python profile12.py
Thu May 5 17:54:49 result
5 function calls in 0.001 CPU seconds
Ordered By:standard Name
Ncalls tottime percall cumtime percall filename:lineno (function)
1 0.000 0.000 0.000 0.000:0 (range)
1 0.000 0.000 0.000 0.000:0 (setprofile)
1 0.000 0.000 0.001 0.001 <string>:1 (<module>)
1 0.001 0.001 0.001 0.001 profile12.py:6 (one)
1 0.000 0.000 0.001 0.001 profile:0 (one ())
0 0.000 0.000 profile:0 (Profiler)

Thu May 5 17:54:49 result
5 function calls in 0.001 CPU seconds
Ordered by:function Name
Ncalls tottime percall cumtime percall filename:lineno (function)
1 0.000 0.000 0.001 0.001 <string>:1 (<module>)
1 0.001 0.001 0.001 0.001 profile12.py:6 (one)
1 0.000 0.000 0.001 0.001 profile:0 (one ())
0 0.000 0.000 profile:0 (Profiler)
1 0.000 0.000 0.000 0.000:0 (range)
1 0.000 0.000 0.000 0.000:0 (setprofile)

Thu May 5 17:54:49 result
5 function calls in 0.001 CPU seconds
Ordered by:cumulative Time
List reduced from 6 to 3 due to restriction <3>
Ncalls tottime percall cumtime percall filename:lineno (function)
1 0.001 0.001 0.001 0.001 profile12.py:6 (one)
1 0.000 0.000 0.001 0.001 profile:0 (one ())
1 0.000 0.000 0.001 0.001 <string>:1 (<module>)
[Email protected] tmp]#

This article is from the "Days Together" blog, please be sure to keep this source http://tongcheng.blog.51cto.com/6214144/1770508

Python module: profile,pstats

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.