Articles Github X Discord Reddit

Python/Pygame performance dump

This is a living document that I update from time to time containing the dos and don’ts to getting useable performance for your Pygame games. There’s some python stuff in here too but we’re mostly here for Pygame.

Before doing anything, you should grab a profiler and run it against your game. I personally use scalene.

Oh and a shameless plug before we begin, my game

Pygame

The last two in particular I’d only pull out when you see a problem. Don’t optimise for the sake of optimising.

Python

Where building lists, dicts, sets etc, use comprehensions.

## Why? Comprehensions are at a C level so are always going to be faster than your `for` loop Don't do: ``` even_numbers = [] for i in range(10): if i % 2 == 0: even_numbers.append(i) ``` Do: ``` even_numbers = [i for i in range(10) if i % 2 == 0] ```