Slow Python - what can be done?

Skip Montanaro skip at pobox.com
Thu Mar 18 19:09:42 CET 2004


    Jason> Here's the code: what should I do to speed things up? 

Without considering your code, two suggestions come to mind.  One, look at
PIL or Numeric.  Perhaps one of them has a more efficient C-based Image
class or array class will will help.  Two, if you're running on an Intel
Pentium-type processor (Windows or Linux), take a look at psyco.

    http://www.pythonware.com/products/pil/
    http://psyco.sourceforge.net/

If you don't have Intel hardware, you might consider PyInline or SciPy's
weave tool to easily inline bits of C code into your Python.

A quick look at your code suggests that you use a fair amount of abstraction
(nothing wrong with that), then throw it away:

    xy = line1.transformPoint(line2, Point(x,y)).to_tuple()
    if self.in_bounds(Point(xy[0], xy[1])):
        ...

In the above, you instantiate a Point using x and y, then pass it to
transformPoint() which returns new Point.  You immediately discard that
point by calling .to_tuple(), then in the next statement create yet another
Point with those same coordinates.  Why not just have transformPoint accept
a tuple and return a tuple or at least save the transformed Point for later
use?

Skip




More information about the Python-list mailing list