小编dhk*_*dhk的帖子

time.perf_counter() 和 time.process_time() 有什么区别?

我正在使用 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)

time python-3.x

23
推荐指数
2
解决办法
2万
查看次数

标签 统计

python-3.x ×1

time ×1