【原創】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://stackoverflow.com/questions/231767/the-python-yield-keyword-explained
Lets continue to part two for next level of quick optimization tricks here.
10. 列表和迭代器 -- 迭代器 具備內存方面的效率和可擴展特性。請使用 itertools 。
建立生成器,並儘量使用 yeild 。他們都比常規列表操做要更快。
相關文章
相關標籤/搜索