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

Re: [pygame] pygame slowness



Hello Lionel,

The above information is provided IMHO.

Reading your statements, it appears your lists are growing very wild.
Check the complexity of your code. Cormen is good reference book:
http://www.amazon.com/gp/product/0262031418/104-6079218-1037556?v=glance&n=283155

If your list growth function is something like n^2 or even worse n^n...you are in trouble no matter what language you choose.
I got this problem when I wrote Invasores, my very simple game. (http://sourceforge.net/projects/invasores).


I did some optimizations, especially on collision detection:

1. Put objects of your Universe on different lists, splitted by class (playes, enemies, background...).
Ex.: when I started to code Invasores, player and enemy object were on the same list.
So to check who collides with who. I had to do an n x n comparison. It wasn't great :-)
But I noticed I don't need to check if enemies collide with each other, just with player objects.
Now, I just check who collides with player's objects. The engine supports 10 times more objects at once.


2. If you have a complex background, try to build it on a separate image and blit the whole image on every frame. It's faster than to blit each piece of background on his own every frame.

3. If you use a tiled background, consider if you can optimize its rendering. Avoid to render many objects on every frame.

4. Avoid very big subscription lists. Try to keep on Events.

5. Python and pygame are fine. It's very good to check if your code is ok.

6. If you want to check collision against a labyrinth or something like that, try using Numeric and a good matrix to check the path.

7. Keep an eye on OO, but not both eyes. Sometimes things go tricky on game programming. :-)



Best Regards,

Nilo Menezes
PS: Sorry about my English.

Lionel barret De Nazaris wrote:

Hello all,
my game (a cross between a shoot them up and gauntlet) is running so slow that i consider abandoning python and pygame (at least for display and collision detection).
I would be very grateful if anybody could lend a hand to help and avoid this, I really like Python.


The heart of the problem in the rendering sub-system. Of 18 sec of play time, 15 of them are spent rendering.
Here follow a short description of the rendering process :
Each iteration of the main loop,
- the Event Manager is called and launch the callbacks linked to the key pressed,
- the World update itself : each World Object update itself (i.e move, shoot and die)
- the Camera update the rendering :
- update it own position in World Coordinates relatively to the World Object it follows (the player's ship)
- calculate for each World Object, the screen position of the image linked to the World Object and blit it.


This seems quite reasonable but I get only 15fps. And the frame rate drop as I add World Objects.
My level are quite empty (around 100 objects, including bullets) and I am afraid that later in the production phase, the engine will be simply too slow to add all the nice things we have in stock.


According to the profile, the main culprit is the Camera Update, but i don't think it's possible to avoid doing calculus for each World Object..So i am quite lost. (Sigh...)

(I joined the profile report to this mail, if you want more details.)

------------------------------------------------------------------------

        518616 function calls (516926 primitive calls) in 10.248 CPU seconds

  Ordered by: internal time, call count
  List reduced from 209 to 50 due to restriction <50>

ncalls tottime percall cumtime percall filename:lineno(function)
72887 1.363 0.000 3.807 0.000 camera.py:36(updateVisualRessource)
72887 1.054 0.000 4.860 0.000 camera.py:54(<lambda>)
25771 0.986 0.000 2.099 0.000 VisualRessource.py:143(update)
29543 0.674 0.000 0.860 0.000 VisualRessource.py:108(draw)
532 0.573 0.001 5.433 0.010 camera.py:21(updateVisualRessourcesFromWorldObjectPosition)
26600 0.514 0.000 1.409 0.000 ressmanager.py:52(<lambda>)
25771 0.511 0.000 0.931 0.000 VisualRessource.py:59(updateVisualRessourceInstanceIFN)
26600 0.387 0.000 0.670 0.000 ressmanager.py:64(<lambda>)
26600 0.348 0.000 0.895 0.000 layer.py:22(updateAndDraw)
26071/25761 0.312 0.000 0.312 0.000 VisualRessource.py:48(isAnyChildModified)
26600 0.283 0.000 0.283 0.000 layer.py:31(clear)
532 0.282 0.001 8.119 0.015 VisualRessourceManager.py:48(update)
532 0.216 0.000 0.886 0.002 VisualRessourceManager.py:61(clear)
25573 0.186 0.000 0.186 0.000 VisualRessource.py:179(getVisible)
25771 0.183 0.000 0.183 0.000 VisualRessource.py:104(getIndexOfClippingRectList)
10801 0.170 0.000 0.170 0.000 VectorMath.py:19(dotProduct)
1345 0.156 0.000 0.387 0.000 collisionmanager.py:90(getWhenPointInRect)
315 0.150 0.000 0.994 0.003 collisionmanager.py:149(getFirstLTCollidedWith)
1260 0.146 0.000 0.424 0.000 collisionmanager.py:27(getCaseCrossdByVector)
2400 0.133 0.000 1.379 0.001 WorldObjectpy:126(update)
5204 0.098 0.000 0.098 0.000 VectorMath.py:99(getIntersectionOf2Segment)
5788 0.077 0.000 0.122 0.000 world.py:70(world)
72 0.068 0.001 0.069 0.001 collisionmanager.py:221(LTAround)
2387 0.066 0.000 0.170 0.000 WorldObjectpy:94(IsInWorld)
1846 0.063 0.000 0.086 0.000 camera.py:84(camera)
1490 0.051 0.000 0.469 0.000 actors.py:82(update)
2684 0.048 0.000 0.136 0.000 collisionmanager.py:99(getVectorOfColl)
5788 0.045 0.000 0.045 0.000 world.py:64(getInstance)
2453/2387 0.045 0.000 1.070 0.000 WorldObjectpy:46(move)
3021 0.045 0.000 0.409 0.000 layer.py:26(<lambda>)
379 0.045 0.000 0.987 0.003 actors.py:173(update)
532 0.042 0.000 0.042 0.000 camera.py:72(clear)
2400 0.042 0.000 1.708 0.001 world.py:46(<lambda>)
1998/894 0.040 0.000 0.221 0.000 effet.py:42(update)
379 0.040 0.000 0.059 0.000 ressdesclibrary.py:87(CreateOwnRessourceInstance)
379 0.034 0.000 0.180 0.000 actors.py:136(update)
1260 0.034 0.000 0.049 0.000 collisionmanager.py:22(getCaseContainingPoint)
1 0.032 0.032 10.248 10.248 main.py:18(main)
2682 0.029 0.000 0.029 0.000 collisionmanager.py:115(<lambda>)
532 0.027 0.000 0.248 0.000 effet.py:10(update)
4020 0.027 0.000 0.027 0.000 VectorMath.py:67(vectorFromPoint)
1846 0.023 0.000 0.023 0.000 camera.py:78(getInstance)
532 0.021 0.000 1.729 0.003 world.py:43(update)
666/656 0.021 0.000 0.109 0.000 VisualRessource.py:36(CreateRessourceInstance)
674 0.021 0.000 0.042 0.000 VectorMath.py:57(Normalize)
2520 0.021 0.000 0.021 0.000 collisionmanager.py:42(<lambda>)
2690 0.020 0.000 0.020 0.000 collisionmanager.py:95(<lambda>)
3094 0.020 0.000 0.020 0.000 log.py:7(log)
532 0.018 0.000 0.090 0.000 eventmanager.py:4(dispatchKeyboardEvent)
778 0.017 0.000 0.039 0.000 collisionmanager.py:143(getTimeOfCollision)


------------------------------------------------------------------------

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.1/204 - Release Date: 15/12/2005