【发布时间】:2013-06-02 12:03:26
【问题描述】:
在我完成最后一些更改后,我的代码变得非常缓慢。搜索任务需要 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 获取有关慢速代码的更多信息?
【问题讨论】:
-
你可以使用一些line-profiler。无论如何,
time.sleep不会为100s睡觉。它正在为0.015s休眠6835次。对time.sleep的近7k 次调用正常吗? -
找到问题的最简单方法is this.
标签: python optimization profile