Reducing pygame cpu-load to < 4 %

Chris Hager chris at linuxuser.at
Sun Dec 9 18:26:14 EST 2007


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):

import sys
import pygame
from pygame.locals import *

def main():
	window = pygame.display.set_mode((400, 225))
	pygame.event.set_blocked(MOUSEMOTION)
	pygame.init()

	while True:
		for event in [ pygame.event.wait() ] + pygame.event.get( ):
			print event
			if event.type == KEYUP:
				# Quit on 'q'
				if event.key == 113: sys.exit(0)

if __name__=="__main__":
    main()


# set_blocked(MOUSEMOTION) has very little influence on the cpu-load,
# but the output is more focused on the other events.

- chris





More information about the Devel mailing list