473,473 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Performance profiling Python code

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/t...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)

Mar 24 '06 #1
2 1479
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/
Mar 24 '06 #2
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.

Mar 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Brian Patterson | last post by:
I have noticed in the book of words that hasattr works by calling getattr and raising an exception if no such attribute exists. If I need the value in any case, am I better off using getattr...
44
by: Carl | last post by:
"Nine Language Performance Round-up: Benchmarking Math & File I/O" http://www.osnews.com/story.php?news_id=5602 I think this is an unfair comparison! I wouldn't dream of developing a numerical...
5
by: Kenneth McDonald | last post by:
Now that I'm back to Python and all the new (to me) cool features, I find I'm using properties a lot, i.e. I'm defining: foo = property(fset=..., fget=...) for a number of properties, in many...
8
by: leo | last post by:
Hello all - I was wondering about the performance implications of explicitly raising exceptions to get information about the current frame. Something like what the inspect module does, with: ...
0
by: Robby Dermody | last post by:
Hey guys (thus begins a book of a post :), I'm in the process of writing a commercial VoIP call monitoring and recording application suite in python and pyrex. Basically, this software sits in a...
3
by: Peter Olcott | last post by:
What can anyone: (1) Tell Me about (2) Provide Links to (3) Recommend Books Pertaining to Memory Performance Optimization? The main thing that I am looking for is rules-of-thumb that maximize...
18
by: koorb | last post by:
Every so often I read something like: "use a defensive code technique like File.Exists instead of a Try and Catch because it's less of a performance hog" And when I program, I obviously have...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.