Python Scripting Performance analysis

Source: Internet
Author: User

From: http://www.cnblogs.com/btchenguang/archive/2012/02/03/2337112.html

deffoo (): Sum=0 forIinchRange (10000): Sum+=I SumA=Bar () Sumb=Bar ()returnsumdefbar (): Sum=0 forIinchRange (100000): Sum+=Ireturnsumif __name__=="__main__":    ImportCProfile#Print analysis results directly to the consoleCprofile.run ("foo ()")    #Save the results of the analysis to a file, but the content is poor readability ... Need to invoke Pstats module analysis resultsCprofile.run ("foo ()","result")    #You can also work directly with the command line    #>python-m cProfile myscript.py-o result         Importpstats#Create a Stats objectp = pstats. Stats ("result")    #The effect of this line is the same as the direct run Cprofile.run ("foo ()").P.strip_dirs (). Sort_stats (-1). Print_stats ()#strip_dirs (): Remove extraneous path information from all module names    #sort_stats (): Sort the printed information according to the standard module/name/line string    #print_stats (): Print out all analysis information     #Sort by Function nameP.strip_dirs (). Sort_stats ("name"). Print_stats ()#Sort by the run time accumulated in a function    #Print_stats (3): Prints only the first 3 lines of function information, the parameter can also be a decimal, representing the first few percent of the function informationP.strip_dirs (). Sort_stats ("Cumulative"). Print_stats (3)     #there is another way to useP.sort_stats (' Time','cum'). Print_stats (. 5,'Foo')    #Sort by time, then by cumulative, and then down to the first 50% with function information     #If you want to know which functions call bar, you can use theP.print_callers (0.5,"Bar")     #Similarly, see which functions are called in the Foo () functionP.print_callees ("Foo")

Run: python-m cProfile t12.py, print result:

The specific explanations for each column of the output are as follows: (http://xianglong.me/article/analysis-python-application-performance-using-cProfile/)

ncalls: Indicates the number of times the function was called;

tottime: Indicates the total elapsed time of the specified function, and removes the run time of the calling child function in the function;

Percall: (the first percall) equals tottime/ncalls;

Cumtime: Represents the time that the call to the function and all its child functions runs, that is, when the function starts calling to the return time;

Percall: (the second percall) is the average time that a function is run, equal to Cumtime/ncalls;

Filename:lineno (function): The specific information of each function call;

In addition, in the analysis above, the sorting method uses the function call time (cumulative), in addition to this there are some other allowable sorting methods:calls, cumulative, file, line, module, name, NFL, pcalls, stdname, time, etc.

# 增加排序方式

python -m cProfile -s cumulative  t2 .py #按照cumulative排序

Python Scripting Performance analysis

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.