当前位置:
首页 > Python基础教程 >
-
python运行时间的几种方法
这篇文章主要为大家详细介绍了python运行时间的几种方法,分析每一种运行时间方法的利弊,感兴趣的小伙伴们可以参考一下
最早见过手写的,类似于下面这种:
import datetime
def time_1():
begin = datetime.datetime.now()
sum = 0
for i in xrange(10000000):
sum = sum + i
end = datetime.datetime.now()
return end-begin
print time_1()
输出如下:
➜ Python python time_1.py
0:00:00.280797
另外一种方法是使用timeit模块,使用方法如下:
In [5]: import timeit
In [6]: timeit.timeit("sum(range(100))")
Out[6]: 1.2272648811340332
还可以在命令行上使用这种timeit模块,如下:
➜ Python python -m timeit -s"import time_1 as t" "t.time_1()"
0:00:00.282044
10 loops, best of 3: 279 msec per loop
注意:timeit模块会多次运行程序以获得更精确的时间,所以需要避免重复执行带来的影响。比方说x.sort()这种操作,因为第一次执行之后,后边已经是排好的了,准确性就收到了影响。
还有一种方法是使用cProfile模块,代码如下,名字为time_1.py:
import datetime
def time_1():
begin = datetime.datetime.now()
sum = 0
for i in xrange(10000000):
sum = sum + i
end = datetime.datetime.now()
return end-begin
if __name__ == '__main__':
print time_1()
import cProfile
cProfile.run('time_1()')
运行程序结果如下:
➜ Python python time_1.py
0:00:00.282828
2 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Traceback (most recent call last):
File "time_1.py", line 15, in <module>
cProfile.run('main()')
File "/usr/lib/python2.7/cProfile.py", line 29, in run
prof = prof.run(statement)
File "/usr/lib/python2.7/cProfile.py", line 135, in run
return self.runctx(cmd, dict, dict)
File "/usr/lib/python2.7/cProfile.py", line 140, in runctx
exec cmd in globals, locals
File "<string>", line 1, in <module>
NameError: name 'main' is not defined
➜ Python vi time_1.py
➜ Python python time_1.py
0:00:00.284642
5 function calls in 0.281 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.281 0.281 <string>:1(<module>)
1 0.281 0.281 0.281 0.281 time_1.py:3(time_1)
2 0.000 0.000 0.000 0.000 {built-in method now}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
复制免费讲解AI专家
➜ Python python time_1.py
0:00:00.282828
2 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Traceback (most recent call last):
File "time_1.py", line 15, in <module>
cProfile.run('main()')
File "/usr/lib/python2.7/cProfile.py", line 29, in run
prof = prof.run(statement)
File "/usr/lib/python2.7/cProfile.py", line 135, in run
return self.runctx(cmd, dict, dict)
File "/usr/lib/python2.7/cProfile.py", line 140, in runctx
exec cmd in globals, locals
File "<string>", line 1, in <module>
NameError: name 'main' is not defined
➜ Python vi time_1.py
➜ Python python time_1.py
0:00:00.284642
5 function calls in 0.281 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.281 0.281 <string>:1(<module>)
1 0.281 0.281 0.281 0.281 time_1.py:3(time_1)
2 0.000 0.000 0.000 0.000 {built-in method now}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
一开始代码里最后一行写的是cProfile.run('main()'),提示没有main(),将main()改成函数名字就可以了。
以上就是本文的全部内容,希望对大家学习python程序设计有所帮助。
来源:https://www.jb51.net/article/86768.htm
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
开启SQLSERVER数据库缓存依赖优化网站性能
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比