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
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西