【发布时间】:2019-02-12 18:36:58
【问题描述】:
我正在使用 Jupyter 笔记本。我正在尝试测量用 python 计算 Avogadro 的数字需要多长时间。我发现time.perf_counter() 和time.process_time() 模块对这类工作很有用。所以我尝试了这两种方法,但结果完全不同。是什么造成了这种差异?这是我的代码。
import time
a = 10 ** 5
def AvogadroCounting():
i = 0
while i <= a:
i += 1
AvogadroCounting()
t_fract = time.perf_counter() #time to count fraction of avogadro's number in Seconds
print(t_fract, 'secs')
我的笔记本给出 693920.393636181 秒。
import time
a = 10 ** 5
def AvogadroCounting():
i = 0
while i <= a:
i += 1
AvogadroCounting()
t_fract = time.process_time() #time to count fraction of avogadro's number in Seconds
print(t_fract, 'secs')
这给出了 2048.768273 秒。
【问题讨论】:
标签: python-3.x time