sampledoc's Scientific Python Tools

Previous topic

Symbolic calculations

Next topic

Timing Your Code

This Page

Optimization of Python Code

Good style for speed [4]:

  1. 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) ]
    
  2. 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)
    
  3. Plain functions are called faster than class methods: trick:

    f = myobj.__call__
    f(x)
    
  4. Inlining functions speeds-up the code

  5. Avoid usieng numpy functions with scalar arguments

  6. Use xrange instead of range (but be aware that xrange only supports a small part of the interface of range() and similar containers. [3])

  7. if-else is faster than try-except (never use exceptions for program flow!)

  8. Avoid resizing of numpy arrays

  9. Callbacks to Python from Fortran/C/C++ are expensive

  10. 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