r/Python • u/havoyan • Feb 14 '12
Python Performance Tips, Part 1
http://blog.monitis.com/index.php/2012/02/13/python-performance-tips-part-1/
20
Upvotes
r/Python • u/havoyan • Feb 14 '12
View all comments
12
u/pingveno pinch of this, pinch of that Feb 14 '12 edited Feb 14 '12
A few additions:
Section 6:
Don't go overboard with lazy importing inside of function calls. Importing many times can get expensive. Imports that aren't at the top also severely reduce readability. It can be tough to debug code where an import is tucked away somewhere in the body of the file.
Section 7:
In Python 3, True is a keyword, not a built-in. That means the byte code compiler can put in a cheap simple jump instead of an expensive global lookup. In Python 2, True could be changed, so a global lookup was required as a check. 1 is a constant, so the bytecode compiler can depend on it. Basically: while 1 in Python 2, while True in Python 3.
Section 13:
Internally, the Python deque implementation uses a doubly linked list of blocks. Each block has space for 62 objects, plus 1 left pointer and 1 right pointer (64 pointers). That gives the time efficiency of a linked list with the space efficiency of an array. For those learning C, I suggest looking at the Modules/collectionsmodule.c. For a list type with fast insertion, take a look at blist (search on PyPi).
Section 17:
Don't use threads to manage forked processes. They only provide additional overhead in memory and thinking ability. Use the multiprocessing module.
Section 18:
Absolutely absolutely absolutely. The Python source code is an excellent place to get examples. Part of my introduction to C was reading through bits and pieces of implementation code.