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

Re: [pygame] Pygame games run slowly :(





On Thu, Jul 30, 2009 at 11:32 AM, Tyler Laing <trinioler@xxxxxxxxx> wrote:
Hi,

Well the first optimization that can be done is to use a lambda function and a list comprehension which performs the for loop at C speed will likely bring a huge benefit:

#we use wrappers because blit and tick only return none, which makes functional programming a bit harder...

f=lambda y: y if y.tick() else y
g=lambda z: z.blit(SCREEN, -CAMERA_X)

particles =[lambda x: f(x) if g(x) else f(x) for x in particles]

Et voila! C-speed execution, O(n) run-time. All it looses is readability, and there's the slight overhead of the two lambda functions... and yes, that else clause is needed in there. Of course for actual production code, I'd assume one would use far more descriptive variables than I did.

You have profiling this vs a normal for loop ?
Somehow, I would expect that in your cycle most of time would be spent in .blit and .tick,  not the loop logic, wich is the only thing this mod speed up.
--
claxo
--