【发布时间】:2014-10-21 01:17:45
【问题描述】:
所以我可以使用time.clock() 来测量函数调用的运行时间:
>>> def f() :
a = time.clock()
range(pow(10,8))
b = time.clock()
print a,b
>>> f()
0.143698 8.345905
但现在如果只是从交互式 shell 中反复调用 time.clock():
>>> time.clock()
0.075492
>>> time.clock()
0.075931
>>> time.clock()
0.076354
>>> time.clock()
0.076754
>>> time.clock()
0.077132
...
这些数字是什么意思?
如果我现在这样做:
>>> def g() :
a = time.clock()
time.sleep(10)
b = time.clock()
print a,b
>>> g()
8.361528 8.361625
好吧,我猜sleep 不计入处理时间,所以这两个数字非常接近。但是8.361528对应的是什么?
我确实阅读了文档,但我仍然不明白:
关于模块时间内置函数时钟的帮助:
时钟(...) 时钟() -> 浮点数
Return the CPU time or real time since the start of the process or since the first call to clock(). This has as much precision as the system records.
【问题讨论】:
-
@jonrsharpe:我一定很笨,但我还是不明白数字 0.077 和 8.36 的含义。
-
@usualme:不要解释绝对值,解释相对差异。这是你唯一关心的想法。
-
time.clock()行为取决于平台。 Usetimeit.default_timer()instead.
标签: python python-2.7 time clock