473,785 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ 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_adjac ent 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_t ile)
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(r andint)
8100 0.160 0.000 0.190 0.000
/usr/lib/python2.4/random.py:149(r andrange)
1 0.000 0.000 0.170 0.170
/home/andreas/openrts/openrts/openrts/common/map.py:26(__ini t__)
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 1500
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
3491
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 within a try statement myself, or is there some clever implementation enhancement which makes this a bad idea? i.e. should I prefer: if hasattr(self,"datum"): datum=getattr("datum") else: datum=None
44
3424
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 application in Python without using prebuilt numerical libraries and data objects such as dictionaries and lists. I have been experimenting with numerical algorithms in Python with a heavy use of the Numeric module. My experience is that Python...
5
3049
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 of my classes. I'm not using them for anything performance critical yet, but could see myself doing so in the future. Can anyone comment on the performance costs associated with properties vs. simple attribute lookup?
8
2026
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: --- def currentframe(): """Return the frame object for the caller's stack frame.""" try:
0
1597
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 VoIP callcenter-type environment (complete with agent phones and VoIP servers), sniffs voice data off of the network, and allows users to listen into calls. It can record calls as well. The project is about a year and 3 months in the making and...
3
2431
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 the execution speed of time critical applications.
18
1193
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 different ideas on how to code different sections. So how to I findout what's best from a performance point-of-view?
19
3155
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 than in .Net environment, under VS 2005 Beta 2. Does anyone have any idea whether this will be addressed in the final release? Thanks, Tomasz
30
3545
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
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
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 we have to send another system

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.