python知识点(一):python检测代码效率常用方法
来源:互联网 发布:数据可视化培训 编辑:程序博客网 时间:2024/06/05 08:44
工作中对大数据需要进行处理,发现某个2重循环运行效率很低,搜集试验了2种比较简单方便检测代码效率的方法如下
1)装饰器来测量函数的执行时间
2)利用库函数cProfile
样例如下:
#coding:utf-8import timefrom functools import wrapsimport cProfiledef fn_timer(function): @wraps(function) def function_timer(*args, **kwargs): t0 = time.time() result = function(*args, **kwargs) t1 = time.time() print ("Total time running %s: %s seconds" % (function.func_name, str(t1-t0)) ) return result return function_timer@fn_timerdef string_match1(a, b, n): for i in range(n): if a is b: c = 'true' else: c = 'false'@fn_timerdef string_match2(a, b, n): for i in range(n): if a == b: c = 'true' else: c = 'false'@fn_timerdef string_match3(a, b, n): for i in range(n): if cmp(a, b): c = 'true' else: c = 'false'def string_match4(a, b, n): for i in range(n): if a is b: c = 'true' else: c = 'false'def string_match5(a, b, n): for i in range(n): if a == b: c = 'true' else: c = 'false'def string_match6(a, b, n): for i in range(n): if cmp(a, b): c = 'true' else: c = 'false' if __name__ == "__main__": n = 10000000 a = 'abcdef' b = 'bcdefg' string_match1(a, b, n) string_match2(a, b, n) string_match3(a, b, n) cProfile.run('string_match4(a, b, n)') cProfile.run('string_match5(a, b, n)') cProfile.run('string_match6(a, b, n)')
运行上述代码后结果为:
Total time running string_match1: 0.505000114441 seconds
Total time running string_match2: 0.487000226974 seconds
Total time running string_match3: 1.15999984741 seconds
4 function calls in 0.444 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.444 0.444 <string>:1(<module>)
1 0.368 0.368 0.444 0.444 test1.py:43(string_match4)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.075 0.075 0.075 0.075 {range}
4 function calls in 0.481 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.481 0.481 <string>:1(<module>)
1 0.422 0.422 0.481 0.481 test1.py:49(string_match5)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.059 0.059 0.059 0.059 {range}
10000004 function calls in 1.816 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.816 1.816 <string>:1(<module>)
1 1.166 1.166 1.816 1.816 test1.py:56(string_match6)
10000000 0.585 0.000 0.585 0.000 {cmp}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.065 0.065 0.065 0.065 {range}
1)3种简单的字符串比较方法中,a is b 最快,使用cmp()函数变慢很多,所以在项目中只需修改一行代码,可以让执行时间缩短至1/3左右
2)使用cProfile来查看运行效率,可以检查内部每个子函数的效率,方便在大型程序中排查问题
- python知识点(一):python检测代码效率常用方法
- Python常用方法(一)
- python 常用知识点
- Python常用知识点
- python 常用知识点总结
- python常用知识点
- [Python]python常用代码
- python 学习知识点一
- python知识点(一)
- Python爬虫知识点一
- python 检测代码性能
- Python项目代码检测
- 机器学习-->python常用知识点
- Python知识点(代码形式)
- 检测Python程序执行效率及内存和CPU使用的7种方法
- 检测Python程序执行效率及内存和CPU使用的7种方法
- 初学python一:零碎知识点
- Python关键知识点整理(一)
- 在sublime text2\3中安装svn插件
- Shellcode的编写
- vc6.0 不能打开文件的问题
- novaclient代码解析之----novaclient创建过程
- JS中使用JSTL
- python知识点(一):python检测代码效率常用方法
- HDU 3861 Prison Breake 状态压缩dp+BFS+二分答案
- 从房东那儿迁来的网线如何用路由器
- 程序员少走弯路的14个忠告
- 架构师害怕程序员知道的十项技能
- 注册信息验证时的用户体验提升
- 系统调用的实现原理
- How Much Bandwidth Does Your Website Really Need
- java知识 HashMap