Performance profiling Python code

Discussion in 'Python' started by =?ISO-8859-1?Q?Andreas_R=F8sdal?=, Mar 24, 2006.

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

    As a start, I'd like to optimize the methods with the highest cumulative
    time spent using CPU, which are the ones on the top of the profiling
    out-put attached below (get_tile_adjacent etc.)

    So are you able to spot any solutions to improving the performance of the
    methods which I've indentified as bottlenecks here?

    Thanks in advance!
    Andreas R.

    The source code of the main faile is here:
    http://svn.gna.org/viewcvs/openrts/trunk/openrts/common/map.py?rev=89&view=markup

    Profiling output is here:

    ncalls tottime percall cumtime percall filename:lineno(function)
    /home/andreas/openrts/openrts/openrts/common/map.py:190(get_tile_adjacent)
    162002 0.680 0.000 0.680 0.000
    /home/andreas/openrts/openrts/openrts/common/map.py:56(get_tile)
    129600 0.570 0.000 0.570 0.000
    /home/andreas/openrts/openrts/openrts/common/map.py:217(wrap_map_pos)
    129837 0.480 0.000 0.480 0.000 :0(append)
    8100 0.190 0.000 0.430 0.000
    /home/andreas/openrts/openrts/openrts/common/map.py:253(get_tile_height_pos)
    8100 0.080 0.000 0.270 0.000
    /usr/lib/python2.4/random.py:213(randint)
    8100 0.160 0.000 0.190 0.000
    /usr/lib/python2.4/random.py:149(randrange)
    1 0.000 0.000 0.170 0.170
    /home/andreas/openrts/openrts/openrts/common/map.py:26(__init__)
    1 0.070 0.070 0.170 0.170
    /home/andreas/openrts/openrts/openrts/common/map.py:32(init_map)
    8100 0.080 0.000 0.110 0.000
    /home/andreas/openrts/openrts/openrts/common/map.py:234(get_tile_height)
     
    =?ISO-8859-1?Q?Andreas_R=F8sdal?=, Mar 24, 2006
    #1
    1. Advertising

  2. 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/
     
    Jack Diederich, Mar 24, 2006
    #2
    1. Advertising

  3. Em Sex, 2006-03-24 às 15:58 -0500, Jack Diederich escreveu:
    > 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.


    Before that, try Psyco. It helps a lot sometimes.

    HTH,

    --
    Felipe.
     
    Felipe Almeida Lessa, Mar 24, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dimitri Ognibene
    Replies:
    0
    Views:
    400
    Dimitri Ognibene
    Apr 25, 2006
  2. Eric S. Johansson

    profiling and performance of shelves

    Eric S. Johansson, Jun 22, 2004, in forum: Python
    Replies:
    0
    Views:
    322
    Eric S. Johansson
    Jun 22, 2004
  3. Eric S. Johansson

    Re: profiling and performance of shelves

    Eric S. Johansson, Jun 25, 2004, in forum: Python
    Replies:
    0
    Views:
    444
    Eric S. Johansson
    Jun 25, 2004
  4. Russell Warren

    Profiling/performance monitoring in win32

    Russell Warren, Feb 17, 2006, in forum: Python
    Replies:
    0
    Views:
    400
    Russell Warren
    Feb 17, 2006
  5. Replies:
    1
    Views:
    319
    Victor Bazarov
    Jun 6, 2007
Loading...

Share This Page