import profile
profile.run('myfunction()','myfunction.profile')
import pstats
pstats.Stats('myfunction.profile').sort_stats('time').print_stats()
显示结果如下:
Thu Jul 03 15:20:26 2003 myfunction.profile
1822 function calls (1792 primitive calls) in 0.737 CPU seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.224 0.224 0.279 0.279 myprogram.py:512(compute)
10 0.078 0.008 0.078 0.008 myprogram.py:234(first)
1 0.077 0.077 0.502 0.502 myprogram.py:249(give_first)
1 0.051 0.051 0.051 0.051 myprogram.py:1315(give_last)
3 0.043 0.014 0.205 0.068 myprogram.py:107(sort)
1 0.039 0.039 0.039 0.039 myprogram.py:55(display)
139 0.034 0.000 0.106 0.001 myprogram.py:239(save)
139 0.030 0.000 0.072 0.001 myprogram.py:314(load)
...
#//python/216