教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 python如何查看程序运行时间

python如何查看程序运行时间

发布时间:2021-03-26   编辑:jiaochengji.com
教程集为您提供python如何查看程序运行时间等资源,欢迎您收藏本站,我们将为您提供最新的python如何查看程序运行时间资源

1、方法一

#python 的标准库手册推荐在任何情况下尽量使用time.clock().
#只计算了程序运行CPU的时间,返回值是浮点数
import time
start =time.clock()
#中间写上代码块
end = time.clock()
print('Running time: %s Seconds'%(end-start))
#运行结果如下
#Running time: 2.26660703157 Seconds

2、方法二

#该方法包含了其他程序使用CPU的时间,返回值是浮点数
import time
start=time.time()
#中间写上代码块
end=time.time()
print('Running time: %s Seconds'%(end-start))
#运行结果
#Running time: 4.90400004387 Seconds

3、方法三

#该方法包含了其他程序使用CPU的时间
import datetime
start=datetime.datetime.now()
#中间写代码块
end=datetime.datetime.now()
print('Running time: %s Seconds'%(end-start))
#运行结果
#Running time: 0:00:02.412000 Seconds

4、方法四

#在 Unix 系统中,建议使用 time.time(),在 Windows 系统中,建议使用 time.clock()
#实现跨平台的精度性可以使用timeit.default_timer()
import timeit
start=timeit.default_timer()
#中间写代码块
end=timeit.default_timer()
print('Running time: %s Seconds'%(end-start))
#运行结果
#Running time: 2.31757675399 Seconds

注释:以上四种代码运行环境是Win7系统,都是在相同的代码块下运行的,可以对比代码运行时间获取windows系统下的最优方法;对于其他系统可以进行测试获取最优方法!

众多python培训视频,尽在python学习网,欢迎在线学习!

您可能感兴趣的文章:
Python是一门怎样的编程语言
python如何查看程序运行时间
linux如何打开python
cython与python的不同有哪些
linux中如何使用python
Python的特点(优点和缺点)
linux如何运行python脚本
python怎么查看安装的模块有哪些
什么是线程(多线程),Python多线程的好处
python语言难学吗

[关闭]
~ ~