[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] pygame slowness



On Fri, Dec 16, 2005 at 03:22:33AM +0900, Guillaume Proux wrote:
> Difficult to help without more information and source code.
> 
> First thing... you seem to have MANY MANY function calls and lambda functions
> ... Python function calling is well known to be slow.
> Try to unroll some of your most inside loop and in this loop make sure
> that all object you are accessing have a local binding.
> 
> Then you can try to use some more helpful optimizations
> I have managed to multiply by 10 the speed of a complex parser I had
> written by going into each function that was too long and making sure
> it was not doing something foolish...
> 
> Sometimes you might find out that in a loop you end up retraversing
> every time a long list...
> Check what happens if you multiply your number of object by 10 in your world.
> If the system goes 10 times slower, then there is some limiting factor
> on the per object basis. If it goes 50-100 times slower... Worries. it
> means that you are doing something expensive in a loop that runs
> square to the number of objects in your world

There's the old joke about optimization:

For the novice: Don't do it.
For the expert: Don't do it yet.

Seriously, the first demo game I wrote had about 100 sprites moving around 
at once (your ship, enemy ships, enemy bullets, background stars, 
powerups, the occasional boss ship that took up a quarter of the screen) 
and I got great frame rates on a Celeron 400MHz machine. I don't think 
pygame is your problem here.

If your game is done, or nearly so, then it's time to optimize. If you've 
got a ways to go, then I'd say get it done first, and then optimize.  
Unfortunately, things that are efficient for programmers to understand are 
often not efficient for computers to process, but until you're done 
there's not much sense in bumming code to improve speed, unless it's so 
slow that you can't even test the functionality effectivley.

Guillaume's advice is good, though.  Watch the amount of function calling 
you do.  Make sure you don't do needless operations.  Do you need to 
frequently find the smallest or largest of some collection of objects?  
Don't sort them, use a heap (Python 2.4 includes a sweet heap 
implementation called heapq, but the min and max heaps are solved 
algorithms and not difficult to implement on your own).  Don't chase a lot 
of object indirection in your inner loops if you don't have to.

I'm not a pygame guru by any stretch of the imagination, but I've used 
python for quite some time, and in general, when I've been dissatsified 
with the performance of my pygame programs, there's usually something I'm 
doing that could be done better.

-- 
Randy Kaelber                                        randy@xxxxxxxxxxxx
Scientific Software Engineer  
Mars Space Flight Facility, Department of Geological Sciences
Arizona State University, Tempe, Arizona, USA