【原创】Python 之快速性能优化(第一部分)

本文为翻译, 原文地址:《 Quick Python Performance Optimization: Part I  

This is part I of a two part series of blog post on performance optimization in python. 
Aim is to just explain, the right way to do simple things which we use in day-to-day python programming, and has a very relevant performance impact. 
此文为关于 Python 性能优化的二连发博文的首篇 ,目标是解释清楚,如何按照正确且简单的方式做好每日 Python 编程,并且能获得好的性能体验。 

1. %timeit (per line) and %prun (cProfile) in ipython interactive shell.  
Profile your code while working on it, and try to find of where is the bottleneck. This is not contrary to the fact that premature optimization is the root of all evil. This is mean for the first level optimization and not a heavy optimization sequence. 
For more on profiling python code, you should read this: http://www.huyng.com/posts/python-performance-analysis/ 
Another interesting library, line_profiler is for line by line profiling https://bitbucket.org/robertkern/line_profiler 
1. 在 ipython 交互式 shell 中使用  %timeit(每行使用)和   %prun(cProfile) 
在你开发代码的过程中时时进行性能测量,努力找出瓶颈所在。这种方式并不与“过早优化是万恶之源”的想法相违背。而是指第一个层次上的优化,而不是重度优化。 
关于对 python 代码的更多性能测量,可以参阅 这里  
另一个有意思的库 --  line_profiler -- 可用于按行性能调优。 

2. Reduce the number of function calls. If you need a list to be manipluated, pass the entire list, rather than iterating over the list and passing  each element to the function and returning. 
2. 减少函数调用次数 。如果你需要对列表进行操作,那么请直接传入整个列表,而不是在列表上做迭代,然后分别将每一个列表元素传入函数再返回。 

3. Use xrange instead of range. (in Python2.x - this is by default in Python3.x) 
xrange is C implementation of range, with an eye on efficient memory usage.  
3. 使用 xrange 替代 range 。(在 Python2.x 中 - 在 Python3.x 中是默认行为) 
xrange 是 range 的 C 实现版本,主要增强了内存使用上的效率。 

4. For huge data, use numpy , its better than standard datastructures. 
4. 对于大块数据,请使用 numpy ,它比标准的数据结构要更优秀。 

5. "".join(string) is better than + or += 
5. "".join(string) 比 + 或 += 更优秀。 

6. while 1 is faster than while True 
6. while 1 比 while True 执行速度更快。 

7. list comphrension > for loop > while loop 
list comprehension is faster than looping over the list, and while loop is the slowest, with an external counter with it. 
7. 列表推导 > for 循环 > while 循环 
列表推导的运行速度快于在列表上做 for 循环,而在列表上做 while 循环是最慢的一种方式,因为其需要额外的计数器。 

8. use cProfile cStringIO and cPickle 
always use available C versions of the modules. 
8. 使用 cProfile、cStringIO 和 cPickle 
总是使用模块的 C 实现版本。 

9. Use local variables . 
local variables are faster than global variables, builtins or attribute lookups 
9. 使用本地变量。 
本地变量要快于全局变量、内置变量,或属性值的查找。 

10. list and iterators versions exist - iterators are memory efficient and scalable. Use itertools 
Create generators and use yeild as much as posible. They are faster compared to the normal list way of doing it. 
http://www.diveinto.org/python3/iterators.html 
http://*.com/questions/231767/the-python-yield-keyword-explained 
Lets continue to part two for next level of quick optimization tricks here. 
10. 列表和迭代器 -- 迭代器 具有内存方面的效率和可扩展特性。请使用 itertools 。 
创建生成器,并尽可能使用 yeild 。他们都比常规列表操作要更快。 
上一篇:IE弹出"是否停止运行此脚本"脚本超时时间设置


下一篇:Python性能优化