[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Whoa! Bad slowdown!



I tried changing the default "fill-in" surface size from 1x1 to 200x200 and removing all the empty 1x1 images in data/characters/noghost, but it had no effect on speed. This error occurred, however, and it seems to be a result of removing the 1x1 images (which caused dynamically generated replacements to be created):

Traceback (most recent call last):
  File "hand2hand.py", line 1920, in <module>
    main()
  File "hand2hand.py", line 1916, in main
    game.start_game()
  File "hand2hand.py", line 511, in start_game
    self.all.update(time_passed)
  File "/usr/lib/python2.7/site-packages/pygame/sprite.py", line 399, in update
    for s in self.sprites(): s.update(*args)
  File "hand2hand.py", line 1789, in update
    self.draw_rightarm()
  File "hand2hand.py", line 1809, in draw_rightarm
    img_a = pygame.surfarray.pixels_alpha(img)
  File "/usr/lib/python2.7/site-packages/pygame/surfarray.py", line 208, in pixels_alpha
    return numpysf.pixels_alpha (surface)
  File "/usr/lib/python2.7/site-packages/pygame/_numpysurfarray.py", line 302, in pixels_alpha
    offset=start, strides=(4, surface.get_pitch ()))
ValueError: strides is incompatible with shape of requested array and size of buffer

At any rate, I used cProfiler and copied the results to a text file. It looks like the draw_rightarm, draw_leftarm, and draw_body methods are the main cause for the slowdown in the update methods. Using some instances of print(), I have isolated the slowdown of these methods to the following piece of code, which occurs in all three:

# Adjust transparency
for row in xrange(0, len(img_a)):
    for j in xrange(0, len(img_a[row])):
        img_a[row][j] = int(img_a[row][j] * alpha_percent)

img_a is a pygame.surfarray.pixels_alpha object. Based on what I found in the documentation, this seemed like the most efficient and easy way to make an image with per-pixel alpha transparency partially transparent everywhere. But besides this slowdown problem that I'm having here, pixels_alpha seems to be causing other problems, as can be seen above. Is there a better way to do what I'm trying to do in that section of code? I was trying to create a motion trail effect.



From: Ian Mallett <geometrian@xxxxxxxxx>
To: pygame-users@xxxxxxxx
Sent: Tue, April 12, 2011 7:53:24 PM
Subject: Re: [pygame] Whoa! Bad slowdown!

Well, for one thing, on my machine, it segfaults constantly. 

I tracked it down to ~line 1006:
scaled = pygame.transform.smoothscale(img, size)
But there doesn't seem to be anything out of the ordinary there.  Maybe trying to resize many 1x1 surfaces to 400x400 leads to a memory leak somewhere in PyGame?

Ian