'Pygame: Efficient way to call draw function in for loops

Here's my problem, I want to draw a grid with pygame so I have this loop in which self.data changes at every time step

    def draw(self, surface):
        for a in range(self.height):
            for b in range(self.width):
                if self.data[a * self.width + b]:
                    pygame.draw.rect(surface, CELL_COLOR,
                                 pygame.Rect(b * CELL_WIDTH, a * CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT))

But this function is called at every time step of my program (ideally 60 times a second) and it really bothers me to have a 'python' for loop in it (it uses 70% of the CPU time)

Is there a more efficient way to do that?

I usually use numpy to speed up such functions but the call to a draw function really bothers me



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source