Performance profiling Python code

Jack Diederich jack at performancedrivers.com
Fri Mar 24 15:58:18 EST 2006


On Fri, Mar 24, 2006 at 09:33:55PM +0100, Andreas R?sdal wrote:
> Hi,
> 
> I'm using the Python profiler to optimize a pathfinding algorithm of a
> game, and would like some help from someone who knows how to improve the
> performance of Python code. The algorithm is A-Star, and it works
> correctly. However, the interface that I made between the A-Star
> pathfinding algorithm and the map data structure is what I've found to be
> a bottle-neck so far.
> 
> So are you able to spot any solutions to improving the performance of the
> methods which I've indentified as bottlenecks here?
> 

You have methods for accessing members, just access the members directly.
Differently worded: don't write try to write Java/C++ in python!
This will save you lots of function call overhead.

For the adjacent tiles you could compute the list once and store it in
the tiles (assuming the tiles don't move around).

As a last resort you can write the sensitive bits in C and add a python
wrapper.  For the ICFP contest[1] each year I write the few primitives
in C and all the logic in python and it works quite well.

-Jack

[1] http://icfpcontest.org/



More information about the Python-list mailing list