Some patterns for fast Python. Know any others?
- Avoid overengineering datastructures. Tuples are better than objects (try namedtuple too though). Prefer simple fields over getter/setter functions.
- Built-in datatypes are your friends. Use more numbers, strings, tuples, lists, sets, dicts. Also check out the collections library, esp. deque.
- Be suspicious of function/method calls; creating a stack frame is expensive.
- Don't write Java (or C++, or Javascript, ...) in Python.
- Are you sure it's too slow? Profile before optimizing!
- The universal speed-up is rewriting small bits of code in C. Do this only when all else fails.
- Avoid overengineering datastructures. Tuples are better than objects (try namedtuple too though). Prefer simple fields over getter/setter functions.
- Built-in datatypes are your friends. Use more numbers, strings, tuples, lists, sets, dicts. Also check out the collections library, esp. deque.
- Be suspicious of function/method calls; creating a stack frame is expensive.
- Don't write Java (or C++, or Javascript, ...) in Python.
- Are you sure it's too slow? Profile before optimizing!
- The universal speed-up is rewriting small bits of code in C. Do this only when all else fails.
View 80 previous comments
Avoid attribute lookups by caching in a local, especially if it's occurring in a high-iteration loop.Sep 11, 2012
+Ralph Corderoy that's a good example of "moving loop invariants out of the loop". Everything that doesn't change can be cached as local variables, even instance methods.
This is one of the optimizations that the pypy JIT does for you.
Worth noting that local variable lookup is faster than global variable or builtin lookup, and much faster than attribute lookup. One micro-optimization is to move everything (even builtins) into local variables. It tends to make your code less readable, so only do it if the performance benefit is really worth it.Sep 11, 2012- I Think "avoid function is very difcult matter.Sep 11, 2012
+Michael Foord, yes, loop-invariant hoisting. Attribute lookup can be particularly painful. The dis module dis() can be fun to see what's going on. Really, locals aren't `looked up', a local is given a slot at compile-time and its index is used from then on.Sep 11, 2012
Universal speed-up is Pypy: speed.pypy.orgSep 11, 2012
Time to close comments.Sep 11, 2012
Commenting is disabled for this post.