python几个实际代码的性能分析
2013-07-13 15:03
471 查看
import profile def test1(): a=None if a==None: print "its none" def test2(): a=None if a is None: print "its none" if __name__=="__main__": profile.run("test1()") profile.run("test2()")
运行结果:
its none
4 function calls in 0.002 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.001 0.001 0.001 0.001 :0(setprofile)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 0.002 0.002 profile:0(test1())
1 0.000 0.000 0.000 0.000 test.py:3(test1)
its none
4 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 :0(setprofile)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 0.000 0.000 profile:0(test2())
1 0.000 0.000 0.000 0.000 test.py:8(test2)
可以看出确实is None的速度远快于==None
再看下字符串连接中使用+和join的区别
from time import time def test5(num): t = time() s = "" list = ['a', 'b', 'b', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'] for i in xrange (num): for substr in list: s += substr print "total run time +:", len(s) print time() - t def test6(num): t = time() s = "" list = num * ['a', 'b', 'b', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'] s = s.join(list) print "total run time join:", len(s) print time() - t if __name__ == "__main__": test5(10) test5(100) test5(1000) test5(10000) test5(100000) print "======================" test6(10) test6(100) test6(1000) test6(10000) test6(100000)
执行结果:
total run time +: 140
0.0
total run time +: 1400
0.0
total run time +: 14000
0.00300002098083
total run time +: 140000
0.0329999923706
total run time +: 1400000
0.417999982834
======================
total run time join: 140
0.0
total run time join: 1400
0.0
total run time join: 14000
0.0
total run time join: 140000
0.00200009346008
total run time join: 1400000
0.0220000743866
字符串长度分别从10到100000线性增长,可以明显看到无论哪个长度,join都具有明显优势
相关文章推荐
- Python代码中的捕捉性能-CPU分析(Python脚本)
- Python代码中的捕捉性能-内存分析
- 使用 profile 进行python代码性能分析
- Python,django:用line_profiler工具分析代码的性能(一)
- Python代码中的捕捉性能-CPU分析(解释器)
- 决策树的Python代码分析(二)
- 通过VS2010性能分析来查找代码中那些地方最损耗资源
- 500G python web、爬虫、数据分析、机器学习、大数据、前端实战项目视频代码免费分享
- Python性能分析工具
- 使用cProfiler对Python程序进行性能分析
- Python代码性能优化技巧
- 决策树的Python代码实现与分析
- Android app native代码性能分析
- Python 代码性能优化技巧(转)
- python 性能分析
- 用Python编写分析Python程序性能的工具的教程
- BT源代码学习心得(八):跟踪服务器(Tracker)的代码分析(用户请求的实际处理)
- Python抓取网页内容应用代码分析
- python回归分析相关代码-散点图,回归,预测
- 使用英特尔® 图形性能分析器分析实际游戏性能