python程序关键路径测试


cProfile——   is recommended for most users; it’s a C extension with reasonable overhead that makes it suitable for profiling long-running programs. Based on  lsprof , contributed by Brett Rosen and Ted Czotter.

profile—— a pure Python module whose interface is imitated by  cProfile . Adds significant overhead to profiled programs. If you’re trying to extend the profiler in some way, the task might be easier with this module.

1. 可以镶嵌到源代码中: 
import cProfile
cProfile.run('foo()')
foo()是执行的函数,此时报告结果会输出到终端。
import cProfile
cProfile.run('foo()', 'fooprof')
将报告结果输出到文件fooprof中

也可不镶嵌到源代码中,直接在终端执行命令:
python -m cProfile myscript.py
报告结果会输出到终端
cProfile.py [-o output_file] [-s sort_order]
-o参数是报告结果输出的文件名。输出到文件是二进制数据。如果要查看二进制的报告结果数据,则新建一个.py文件,导入
import pstats
p = pstats.Stats('fooprof')
p.strip_dirs().sort_stats(-1).print_stats()
import pstats
p = pstats.Stats('fooprof')

The class Stats (the above code just created an instance of this class) has a variety of methods for manipulating and printing the data that was just read into p. When you ran cProfile.run() above, what was printed was the result of three method calls:

p.strip_dirs().sort_stats(-1).print_stats()

The first method removed the extraneous path from all the module names. The second method sorted all the entries according to the standard module/line/name string that is printed. The third method printed out all the statistics. You might try the following sort calls:

p.sort_stats('name')
p.print_stats()

The first call will actually sort the list by function name, and the second call will print out the statistics. The following are some interesting calls to experiment with:

p.sort_stats('cumulative').print_stats(10)

This sorts the profile by cumulative time in a function, and then only prints the ten most significant lines. If you want to understand what algorithms are taking time, the above line is what you would use.

If you were looking to see what functions were looping a lot, and taking a lot of time, you would do:

p.sort_stats('time').print_stats(10)

to sort according to time spent within each function, and then print the statistics for the top ten functions.

You might also try:

p.sort_stats('file').print_stats('__init__')

This will sort all the statistics by file name, and then print out statistics for only the class init methods (since they are spelled with __init__ in them). As one final example, you could try:

p.sort_stats('time', 'cum').print_stats(.5, 'init')

This line sorts statistics with a primary key of time, and a secondary key of cumulative time, and then prints out some of the statistics. To be specific, the list is first culled down to 50% (re: .5) of its original size, then only lines containing init are maintained, and that sub-sub-list is printed.

If you wondered what functions called the above functions, you could now (p is still sorted according to the last criteria) do:

p.print_callers(.5, 'init')

and you would get a list of callers for each of the listed functions.

If you want more functionality, you’re going to have to read the manual, or guess what the following functions do:

p.print_callees()
p.add('fooprof')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值