Python中的time.perf_counter()函数
在本教程中,我们将学习time.perf_counter()方法。
方法time.perf_counter()返回以秒为单位的时间浮点值。让我们来看看
示例
# importing the time module import time # printing the time print(time.perf_counter())
输出结果
如果运行上面的代码,则将得到以下结果。
263.3530349
我们可以使用time.perf_counter()方法来查找程序的执行时间。让我们来看一个例子。
示例
#导入时间模块
import time
#求素数的程序
def is_prime(number):
for i in range(2, number):
if number % i == 0:
return False
return True
if __name__ == '__main__':
number = 17377
start_time = time.perf_counter()
is_prime(number)
end_time = time.perf_counter()
#按差异打印执行时间
print(end_time - start_time)输出结果
如果执行上述程序,则将得到以下结果。
0.004171799999994619
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短