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 生日广州男生祝福语简短