我正在使用 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')
Run Code Online (Sandbox Code Playgroud)
我的笔记本给出了 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') …Run Code Online (Sandbox Code Playgroud)