Python cProfile 的开销很大吗?

发布于 2024-09-07 17:55:52 字数 511 浏览 7 评论 0原文

各位 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眼睛不忧郁 2024-09-14 17:55:52

因为它做了更多的工作? time 只是对整个操作进行计时,而 cProfile 在检测下运行它,以便获得详细的细分。显然,分析并不意味着在生产中使用,因此 2.5 倍的开销似乎只是一个很小的代价。

Because it's doing a lot more work? time just times the whole operation, while cProfile 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.

旧人哭 2024-09-14 17:55:52

函数f返回得非常快。当您使用 cProfile 时,归因于对 f 的一次调用的时间并不准确,因为该时间非常小,以至于与测量时间的误差相当。用于测量时间差异的时钟可能只能精确到 0.001 秒。因此,每次测量的误差可能比您尝试测量的时间大几个数量级。这样做 1e7 次,你就会得到虚假的结果。 (参见http://docs.python.org/library/profile.html#limitations< /a> 对此进行更多讨论。)

请注意,如果您更改要使用的代码

def f(a, b):
 for i in xrange(int(1e4)):    
     c = a+b

# a simple loop
def loop():
 for i in xrange(int(1e3)):
  f(1,2)

Time taken 0.732 s.
         1003 function calls in 0.725 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.725    0.725 <string>:1(<module>)
     1000    0.723    0.001    0.723    0.001 test.py:4(f)
        1    0.001    0.001    0.725    0.725 test.py:9(loop)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

您将执行相同数量的循环,但每次调用 f 需要更长的时间。这减少了每次测量的误差。 (每次调用 f 所花费的时间都包含一个错误,该错误现在并不像测量的总时间那么大。)

The function f returns very quickly. When you use cProfile, the time being attributed to one call to f 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

def f(a, b):
 for i in xrange(int(1e4)):    
     c = a+b

# a simple loop
def loop():
 for i in xrange(int(1e3)):
  f(1,2)

you get

Time taken 0.732 s.
         1003 function calls in 0.725 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.725    0.725 <string>:1(<module>)
     1000    0.723    0.001    0.723    0.001 test.py:4(f)
        1    0.001    0.001    0.725    0.725 test.py:9(loop)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

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 to f contains an error which is now not as large the total time measured.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文