[OLPC-Games] Reducing pygame cpu-load to < 4 %

Mike C. Fletcher mcfletch at vrplumber.com
Sun Dec 9 18:36:10 EST 2007


Chris Ball wrote:
> Hi,
>
>    > Hi!  Mcfletch and I have been talking about the heavy cpu-load of
>    > the wiki's pygame demo: 99%; and how to reduce it.
>
>    > We figured out, a combination of pygame.event.wait() and
>    > pygame.event.get() very good. That's the demo code; it takes less
>    > than 4% cpu (0.3% - 1.7% here):
>
> That's cool.  Note that this won't work for any pygame activity that
> wants to do something in the background, though, such as the examples
> that Pippy now ships with[1] -- you'll now only enter the event loop
> when a keypress happens, even if you've set up a default case to move
> something around when there isn't new input to handle.
>
> - Chris.
>
> [1]:  http://dev.laptop.org/~cjb/pippy/Pippy-11.xo , with thanks to
>       Henry Holtzman for the new pygame examples.
>   
You should be able to prompt a new rendering cycle when such movement
occurs like so:

    pygame.event.post( pygame.event.Event( pygame.USEREVENT ) )

thus if you've got a discrete simulation (e.g. happens every .5
seconds), you can send the user event when you've completed the
simulation tick and then go completely quiescent waiting for the next
user interaction or simulation tick.  You could wrap such code in a
"show" method in your Activity so that people can call it when they want
their code to show up as graphics.

At least, that's the theory :) ,
Mike

-- 
________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com



More information about the Games mailing list