天天看點

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

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) 

在你開發代碼的過程中時時進行性能測量,努力找出瓶頸所在。這種方式并不與“過早優化是萬惡之源”的想法相違背。而是指第一個層次上的優化,而不是重度優化。 

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.