Skip to main content

Optimisations — wiki

Here is a list of optimisations for pygames.

Main optimisations - this is for those optimisations which are of most benefit for pygames.

Special case optimisations - for optimisations which are useful for some games.

Python optimisations - For python related optimisations.

Fake it to you make it - Ways to approximate things quickly instead of doing them properly.

Main optimisations

Use convert() and convert_alpha() after loading images so that the surface is converted into the display format for fastest blitting.

Use dirty rects to only update parts of the screen which have changed.

You can use the sprite.RenderUpdates group to do this easily. This can have a seriously good impact on frame rates. If the entire screen is being updated every frame, then dirty rects won't help.

Special case optimisations

Reducing load time can be done by instead of having 100 .jpg or .png files instead you have all of the images in a larger image. That way you load only one file instead of 100.

Python optimisations

Python itself is a bottleneck when you are iterating over large numbers of objects(tiles on a map, or hundreds of sprites). The easiest thing to do is to avoid these situations, by lowering your requirements for resolution or framerate.

Things to try if the requirements can't be changed: import the Psyco module and run psyco.full(). Change the function calls you are using, or reduce the number of functions called. Always use xrange instead of range. Enable the Python profiler to identify the most critical segments and bring their relative share of processing time down. Learn OpenGL for 2d to clear up rendering bottlenecks. (this is not so difficult if you already know SDL and are willing to do some research).