Python cProfile 的开销很大吗?
各位 Python 专家,大家好,我开始使用 cProfile,以便获得有关我的程序的更详细的计时信息。然而,令我感到相当不安的是,开销很大。知道为什么 cProfile 报告 7 秒而时间模块在下面的代码中只报告 2 秒吗?
# a simple function
def f(a, b):
c = a+b
# a simple loop
def loop():
for i in xrange(10000000):
f(1,2)
# timing using time module
# 2 seconds on my computer
from time import time
x = time()
loop()
y = time()
print 'Time taken %.3f s.' % (y-x)
# timing using cProfile
# 7 seconds on my computer
import cProfile
cProfile.runctx('loop()', globals(), locals())
Hi expert Pythonists out there, I am starting to use cProfile so as to have a more detailed timing information on my program. However, it's quite disturbing to me that there's a significant overhead. Any idea why cProfile reported 7 seconds while time module only reported 2 seconds in the code below?
# a simple function
def f(a, b):
c = a+b
# a simple loop
def loop():
for i in xrange(10000000):
f(1,2)
# timing using time module
# 2 seconds on my computer
from time import time
x = time()
loop()
y = time()
print 'Time taken %.3f s.' % (y-x)
# timing using cProfile
# 7 seconds on my computer
import cProfile
cProfile.runctx('loop()', globals(), locals())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为它做了更多的工作?
time
只是对整个操作进行计时,而cProfile
在检测下运行它,以便获得详细的细分。显然,分析并不意味着在生产中使用,因此 2.5 倍的开销似乎只是一个很小的代价。Because it's doing a lot more work?
time
just times the whole operation, whilecProfile
runs it under instrumentation so it can get a detailed breakdown. Obviously, profiling is not meant to be used in production, so a 2.5x overhead seems like a small price to pay.函数
f
返回得非常快。当您使用 cProfile 时,归因于对f
的一次调用的时间并不准确,因为该时间非常小,以至于与测量时间的误差相当。用于测量时间差异的时钟可能只能精确到 0.001 秒。因此,每次测量的误差可能比您尝试测量的时间大几个数量级。这样做 1e7 次,你就会得到虚假的结果。 (参见http://docs.python.org/library/profile.html#limitations< /a> 对此进行更多讨论。)请注意,如果您更改要使用的代码
,
您将执行相同数量的循环,但每次调用
f
需要更长的时间。这减少了每次测量的误差。 (每次调用f
所花费的时间都包含一个错误,该错误现在并不像测量的总时间那么大。)The function
f
returns very quickly. When you use cProfile, the time being attributed to one call tof
is not accurate because the time is so small that it is comparable to the error in measuring time. The clock used to measure differences in time may only be accurate to 0.001s. So the error in each measurement may be orders of magnitude greater that the time you are trying to measure. Do this 1e7 times and you've got bogus results. (See http://docs.python.org/library/profile.html#limitations for more discussion of this.)Notice that if you change the code to use
you get
You are doing the same number of loops, but each call to
f
takes longer. This cuts down on the error per measurement. (The time attributed to each call tof
contains an error which is now not as large the total time measured.)