在本教程中,我们将学习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
最后
以上就是精明小伙最近收集整理的关于python time perf_Python中的time.perf_counter()函数的全部内容,更多相关python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复