我的代码在我做了一些最后的修改后变得非常慢。一个搜索任务需要102秒而不是2-3秒。
我尝试使用profile
类来找到限制函数,下面是输出:
>>> import WebParser
>>>
>>> w = WebParser.LinksGrabber
>>>
>>> import cProfile
>>> cProfile.run("w.search('nicki minaj', 15)")
50326 function calls in 102.745 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 102.745 102.745 <string>:1(<module>)
6 0.000 0.000 0.000 0.000 Config.py:110(__getattr__)
1 0.000 0.000 102.745 102.745 LinksGrabber.py:427(search)
5 0.000 0.000 0.002 0.000 Queue.py:107(put)
911 0.040 0.000 102.726 0.113 Queue.py:150(get)
..................................
}
6836 0.022 0.000 0.022 0.000 {min}
917 0.009 0.000 0.009 0.000 {thread.allocate_lock}
3 0.000 0.000 0.000 0.000 {thread.get_ident}
3 0.000 0.000 0.000 0.000 {thread.start_new_thread}
6835 100.458 0.015 100.458 0.015 {time.sleep}
11346 0.035 0.000 0.035 0.000 {time.time}
它显示一个time.sleep
代码正在等待100.458s
,但是我在我的WebParser.LinksGrabber
类中找不到该代码段。
我如何使用profile
来获得关于慢代码段的更多信息?
我有一段样板代码添加到需要评测的程序中,我可以通过将
PROFILE
变量改为True
或False
来轻松启用或禁用它。。。在…它创建一个文本文件
profile.txt
,其中包含。。。在…看起来像这样。。。在
^{pr2}$…这通常足以跟踪代码需要改进的地方。在
相关问题 更多 >
编程相关推荐