Python效能最佳化經驗之談(翻譯)

來源:互聯網
上載者:User


在微博上看到了一遍文章,然後就試著翻譯了一下,有些地方看不懂,就直接貼原文了,還望能看懂的人指點一下。

# 快速的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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.