我用的是Jupyter笔记本.我试图测量用python计算阿伏加德罗的数字需要多长时间.我发现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.3936181秒.
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秒.