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

Re: [pygame] High CPU usage drawing static boxes



JoÃo Pinto schrieb:
>     I can't see a measurable difference in CPU usage here.  Intel Core 2
>     Duo @ 2.4 GHz.
> 
> 
> Hello Ian,
> that's odd. On an Intel Core i7 CPU 920  @ 2.67GHz the python process is
> using around 25% CPU (from a single core).

I can confirm the high CPU load here (OS X w/ Core Duo 1.8 Ghz).

Your code can be optimized in several way. Each display update draws the
whole LetterBoard and each LetterBox in it. The LetterBox.draw() method
isn't very efficient, because it basically re-creates the tile on each draw.

- Save the current state of the LetterBox Surface and use it for the
next re-draw until it actually has to change.

- Use display.update() and only draw things that have actually changed
on each frame update.

- Convert the tile images after loading them. See
http://www.pygame.org/docs/ref/image.html#pygame.image.load

- Decouple event handling from the display refesh rate. For many
applications you don't actually need a high display refresh rate. Use
the Publisher/Subscriber object design pattern and have a PygameDisplay
object that subscribes to "display update" events. In your main loop,
post "display update" events to the EventDispatcher (aka Publisher)
every second/refresh rate. Your event loop can then run at a much higher
rate, to handle input events in a more responsive manner, if you desire.

Chris