【发布时间】:2010-09-10 06:30:08
【问题描述】:
在 Python 中哪个更好用于计时? time.clock() 还是 time.time()?哪个更准确?
例如:
start = time.clock()
... do something
elapsed = (time.clock() - start)
对比
start = time.time()
... do something
elapsed = (time.time() - start)
【问题讨论】:
-
请注意,从 Python 3.3 开始,the use of
time.clock()is deprecated,建议改用perf_counter()或process_time()。 -
@CodyPiersall:即使在 Python 2 上,您也应该 use
timeit.default_timer()to measure performance (it is assigned to time.time() or time.clock() depending on OS)。 -
请参阅this comment,了解进程使用的所有内核如何在
time.clock和time.process_time中求和,但子进程不是。另见this discussion of precision(当然,因系统而异)。