我目前正在学习如何使用cProfile
,我有一些疑问。
我目前正在尝试分析以下脚本:
import time
def fast():
print("Fast!")
def slow():
time.sleep(3)
print("Slow!")
def medium():
time.sleep(0.5)
print("Medium!")
fast()
slow()
medium()
我执行命令python -m cProfile test_cprofile.py
,我得到以下结果:
Fast!
Slow!
Medium!
7 function calls in 3.504 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 3.504 3.504 test_cprofile.py:1(<module>)
1 0.000 0.000 0.501 0.501 test_cprofile.py:10(medium)
1 0.000 0.000 0.000 0.000 test_cprofile.py:3(fast)
1 0.000 0.000 3.003 3.003 test_cprofile.py:6(slow)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
2 3.504 1.752 3.504 1.752 {time.sleep}
但是,当我在顶部使用 pylab 导入(例如(import pylab
))编辑脚本时,输出cProfile
非常大。我试图限制使用的行数,python -m cProfile test_cprofile.py | head -n 10
但是我收到以下错误:
Traceback (most recent call last):
File "/home/user/anaconda/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/home/user/anaconda/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/user/anaconda/lib/python2.7/cProfile.py", line 199, in <module>
main()
File "/home/user/anaconda/lib/python2.7/cProfile.py", line 192, in main
runctx(code, globs, None, options.outfile, options.sort)
File "/home/user/anaconda/lib/python2.7/cProfile.py", line 56, in runctx
result = prof.print_stats(sort)
File "/home/user/anaconda/lib/python2.7/cProfile.py", line 81, in print_stats
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
File "/home/user/anaconda/lib/python2.7/pstats.py", line 360, in print_stats
self.print_line(func)
File "/home/user/anaconda/lib/python2.7/pstats.py", line 438, in print_line
print >> self.stream, c.rjust(9),
IOError: [Errno 32] Broken pipe
有人可以帮助解决与此类似的情况的正确程序,在这种情况下,我们有一个import pylab
或另一个模块可以生成如此高的输出信息cProfile
吗?