Good style for speed [4]:
Avoid explicit loops: use vectorizd numpy expressions instead. If you really need a loop, then try list comprehensions as they are in general much faster. Example:
1 2 3 4 5 6 7 8 9 10 11 12 # Slow: x = [] for i in xrange(8): x.append( i**2 ) # Better: x = [0, 0, 0, 0, 0, 0, 0, 0] for i in xrange(len(x)): x[i] = i**2 # Best: x = [ i**2 for i in xrange(8) ]Avoid module prefix in frequently called funcions:
import mod func = mod.func for x in hugelist: func(x)Or even import the function into the global namespace:
from mod import func for x in hugelist: func(x)Plain functions are called faster than class methods: trick:
f = myobj.__call__ f(x)Inlining functions speeds-up the code
Avoid usieng numpy functions with scalar arguments
Use xrange instead of range (but be aware that xrange only supports a small part of the interface of range() and similar containers. [3])
if-else is faster than try-except (never use exceptions for program flow!)
Avoid resizing of numpy arrays
Callbacks to Python from Fortran/C/C++ are expensive
Avoid unnecessary allocation of large data structures
11. Be careful with type mactching in mixed Python-Fortran software (e.g. flat/ real*8): if the array entry types do not match, arrays will be copied!
12. Use asarray with parameter order=’Fortran’ when sending arrays from numpy to Fortran routines!
Footnotes
[3] | Objects of type xrange don’t support slicing, concatenation or repetition, xrange objects have very little behavior: they only support indexing, iteration, and the len() function. The advantage of the xrange type is that an xrange object will always take the same amount of memory, no matter the size of the range it represents. (This is some kind of lazy evaluation.) http://docs.python.org/library/functions.html#xrange |
[4] | See also http://wiki.python.org/moin/PythonSpeed/PerformanceTips |