1

Right now, pygame is doing many, many computations in order to render my current project. I was told that NumPy could help "un-bottleneck" my project. I have some ideas as to how it can be implemented, but I was wondering if you guys have any ideas/suggestions as to how NumPy can be used to improve the performance of this update function, that would be nice :)

Description of the function: As an object (i.e. ball) moves around the hitGrid, it tracks where the object is via its position and subtracts the value of that position in hitGrid by one.

My Code:

# Variables
WIDTH, HEIGHT, MARGIN = 10, 10, 1
GRIDX, GRIDY = 100, 40
def update(self, boundrect, hitGrid, hitList):
    self.pos += self.vel
    self.rect.center = self.pos
    
    if self.rect.left <= boundrect.left or self.rect.right >= boundrect.right:
        self.vel.x *= -1                            
    if self.rect.top <= boundrect.top or self.rect.bottom >= boundrect.bottom:
        self.vel.y *= -1     

    # align rect to grid
    gridpos = round(self.rect.x / (WIDTH+MARGIN)), round(self.rect.y / (HEIGHT+MARGIN))
    self.rect.topleft = gridpos[0] * (WIDTH+MARGIN), gridpos[1] * (HEIGHT+MARGIN)

    # increment touched filed
    global max_hit
    max_hit = 100
    
    #numpy used here
    oldHitList = hitList[:]
    hitList.clear()
    for c in range(self.gridsize[0]):
        for r in range(self.gridsize[1]):
            p = gridpos[1] + r, gridpos[0] + c
            if p in oldHitList:
                hitList.append(p)
            elif self.grid[r][c] == 1:
                if p[0] < len(hitGrid) and p[1] < len(hitGrid[p[0]]):
                    hitList.append(p)
                    if p not in oldHitList:
                        hitGrid[p[0]][p[1]] -= 1
                        max_hit = min(max_hit, hitGrid[p[0]][p[1]])
# object used by gridObject
ballGrid = [[1]]

# How gridObject is initialized in main()
def main():
...
    sprite_group = pygame.sprite.Group()
    ball = GridObject((screen.get_width()//2, screen.get_height()//2), ballGrid, sprite_group)
    hitGrid = [[100 for i in range(GRIDX)] for j in range(GRIDY)]
    hitList = []
...
...
...
        # How the update fuction is used
        bounding_box = pygame.Rect(0,0,(GRIDX * (WIDTH + MARGIN) + MARGIN), (GRIDY * (HEIGHT + MARGIN)))
        sprite_group.update(bounding_box, hitGrid, hitList)

My thoughts:

# in update
def update(self, boundrect, hitGrid, hitList):
...
   oldHitList = hitList[:]
   # How do you clear a np.array like you would a list?
   hitList.clear() # hitList = np.empty((GRIDX, GRIDY))?
   for c in range(self.gridsize[0]):
       for r in range(self.gridsize[1]):
           p = gridpos[1] + r, gridpos[0] + c
           if p in oldHitList:
               hitList.append(p) # hitList = np.append(hitList, p)
           elif self.grid[r][c] == 1:
               if p[0] < len(hitGrid) and p[1] < len(hitGrid[p[0]]):
                   hitList.append(p) # hitList = np.append(hitList, p)
                   if p not in oldHitList:
                       hitGrid[p[0]][p[1]] -= 1
                       max_hit = min(max_hit, hitGrid[p[0]][p[1]])
...
# in main
hitGrid = np.full((GRIDX, GRIDY), max_hit)
hitList = np.empty((GRIDX, GRIDY))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • It would help to include a conceptual description of what the "update" entails. – Karl Knechtel Nov 17 '20 at 23:39
  • If you need more info, let me know – WebDevStudent Nov 18 '20 at 00:23
  • 1
    You have asked and deleted exactly the same question some days ago. What has changed? It is not intended to ask the same question twice. - [How do I use numpy arrays instead of lists to improve the performance of my PyGame application?](https://stackoverflow.com/questions/64849625/how-do-i-use-numpy-arrays-instead-of-lists-to-improve-the-performance-of-my-pyga) – Rabbid76 Nov 18 '20 at 05:59
  • 2
    Could you provide a runnable example? I have a hard time understanding what you're trying to do here. What's a `hitGrid`? So you have an object that moves around on a 100x40 grid, and everytime it moves to a new tile in that grid, that tile's value is decreased by 1? Am I interpreting it correctly? – sloth Nov 18 '20 at 07:37
  • @sloth yes you are – WebDevStudent Nov 18 '20 at 09:04

0 Answers0