Python comes with several performance Analysis modules: Profile, Cprofile, and Hotshot, all of which are basically the same way, nothing more than a pure Python or C-written module. This article describes cprofile.
Example
Import Time def func1 (): = 0 for in range (1000000): + = idef Func2 (): time.sleep (ten) func1 () Func2 ()
Run
del. py
Run results
Results analysis
Performed 6 functions, which cost a total of 10.138s, sorted by the name of the running function as the result output.
Run the script
del del. py
This module directly saves the profile results, which can be used to further analyze the output results and run
" import pstats; p=pstats. Stats (' del.out '); P.print_stats ()"
Result (Random)
You can set the sort by how much time it takes to sort
" import pstats; p=pstats. Stats (' del.out '); P.sort_stats (' time '). Print_stats ()"
Sort_stats supports the following parameters:
Calls, cumulative, file, line, module, name, NFL, Pcalls, Stdname, time
The Pstats module also supports interactive
Cprofile--python Performance analysis Tools