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 圣诞简短祝福语给父母