在微博上看到了一遍文章,然後就試著翻譯了一下,有些地方看不懂,就直接貼原文了,還望能看懂的人指點一下。
# 快速的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
2.減少函數的調用次數
如果需要對一個list進行操作,向函數傳入一個list要比每個元素調用一次函數快。
3.使用xrange代替range ##
xrange是range的C實現,更高效的使用記憶體
4.對於大資料,使用numpy要比標準資料結構快
5."".join(string) 比 + 或者 += 好
6.while 1 比 while True 快
7.列表推導式 > for 迴圈 > while 迴圈
列表推導式比for迴圈快,while迴圈是最慢的,因為while使用外部計數器
8.使用 cProfile, cStringIO 和 cPickle
始終使用可用的C版本的模組。
9.使用局部變數 局部變數比全域變數,宏和屬性尋找快
10.ist 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.
11.在所有可能的地方,使用 Map, Reduce and Filter 代替迴圈. 12.對於檢查'a in b'的地方, dict or set 比 list/tuple好.
13.對於大資料,儘可能的使用不可變類型,這樣更快 tuples>list
14.insertion into a list in O(n) complexity.
15.如果要從首尾巨集指令清單,使用雙端隊列
16.使用del刪除使用後的對象
Python does it by itself. But make sure of that with the gc module or
by writing an __del__ magic function or
the simplest way, del after use.
1.time.clock()
18.GIL(http://wiki.python.org/moin/GlobalInterpreterLock) - GIL is a demon.
GIL allows only one python native thread to be run per process, preventing CPU level parallelism. Try using ctypes and native C libararies to overcome this. When even you reach the end of optimizing with python, always there exist an option of rewriting terribly slow functions in native C, and using it through python C bindings. Other libraries like gevent is also attacking the problem, and is successful to some extend.
TL,DR: While you write code, just give one round of thought on the data structures, the iteration constructs, builtins and create C extensions for tricking the GIL if need.