Re: [pygame] New on the mailing list with a strange FPS behavior

2007-11-12 Thread Noah Kantrowitz
Ghislain Leveque wrote:
 2007/11/12, claxo [EMAIL PROTECTED]:
   
 On 11 Nov 2007 at 22:41, Ghislain Leveque wrote:
 
 Today I've come into a strange behavior with my rendering loop.
   

   
 3. Memory garbage collection kicks in. you can try to do a explicit
 garbage collection after each M frames to see if max time is reduced.
 

 How do I do an explicite garbage collection ?

   
Normal collection is run continuously (reference counting). The periodic
part is just for cycle detection. Don't have reference loops.

--Noah



signature.asc
Description: OpenPGP digital signature


Re: [pygame] New on the mailing list with a strange FPS behavior

2007-11-12 Thread claxo
Look at the module gc in the global module index of Python docs, but
in a pinch:

import gc
...

gc.collect( )

Don't put into a tight loop, this waste cpu time.

claxo


On 12 Nov 2007 at 8:30, Ghislain Leveque wrote:

 2007/11/12, claxo [EMAIL PROTECTED]:
  On 11 Nov 2007 at 22:41, Ghislain Leveque wrote:
   Today I've come into a strange behavior with my rendering loop.

  3. Memory garbage collection kicks in. you can try to do a explicit
  garbage collection after each M frames to see if max time is reduced.

 How do I do an explicite garbage collection ?

 Thanks

 --
 Ghislain Lévêque






Re: [pygame] New on the mailing list with a strange FPS behavior

2007-11-12 Thread claxo
I seems to remember time.time() is prefered to calculate delta times?
Anybody ?

On 12 Nov 2007 at 8:29, Ghislain Leveque wrote:

 First I will explain how I did measure and what make me think it's the
 draw that stucks :
 ...
 def post(self,event):
   for listener in self.listeners:
 now = pygame.time.get_ticks()
 listener.notify(event)
 self.stats[listener].append((pygame.time.get_ticks() - now,event))
 Ghislain Lévêque




Re: [pygame] New on the mailing list with a strange FPS behavior

2007-11-12 Thread Simon Wittber
On Nov 12, 2007 4:29 PM, Ghislain Leveque [EMAIL PROTECTED] wrote:

 And I can see the freezing.

Put a time.sleep(0) call in your inner loop. The problem will likely go away.


-- 
:: Simon Wittber
:: http://www.linkedin.com/in/simonwittber
:: phone: +61.4.0135.0685
:: jabber/msn: [EMAIL PROTECTED]


[pygame] New on the mailing list with a strange FPS behavior

2007-11-11 Thread Ghislain Leveque
Hi there,

I subscribed to the mailing list today because I've used pygame for a
while now and I begin to be comfortable with it.

Today I've come into a strange behavior with my rendering loop.

I use dirty rectangles to update the screen so my render function
looks like this :

def draw(self):
self.spritegroup1.clear(self.window,self.background)
self.spritegroup2.clear(self.window,self.background)
# ...

dirty = self.spritegroup1.draw(self.window) +
self.spritegroup2.draw(self.window) # ...
pygame.display.update(dirty)
self.clock.tick (TARGET_FPS)

I've measured the time to each call to this draw function and the
normal time is 15ms (quite fast) but from time to time, the function
takes 500ms to execute !! And of course I see my game freezing...

Can anyone tell me where I should look at ?
-- 
Ghislain Lévêque


Re: [pygame] New on the mailing list with a strange FPS behavior

2007-11-11 Thread claxo


On 11 Nov 2007 at 22:41, Ghislain Leveque wrote:

 Hi there,

 I subscribed to the mailing list today because I've used pygame for a
 while now and I begin to be comfortable with it.

Wellcome!

 Today I've come into a strange behavior with my rendering loop.

 I use dirty rectangles to update the screen so my render function
 looks like this :

 def draw(self):
 self.spritegroup1.clear(self.window,self.background)
 self.spritegroup2.clear(self.window,self.background)
 # ...

 dirty = self.spritegroup1.draw(self.window) +
 self.spritegroup2.draw(self.window) # ...
 pygame.display.update(dirty)
 self.clock.tick (TARGET_FPS)

 I've measured the time to each call to this draw function and the
 normal time is 15ms (quite fast) but from time to time, the function
 takes 500ms to execute !! And of course I see my game freezing...

 Can anyone tell me where I should look at ?
 --
 Ghislain Lévêque


1. I don't known if clock.tick() can be to blame, maybe you can try
to bypass clock.tick to see if times are irregular.

2. If the size or quantity of sprites have great variations, so can
the time. And if the workload is too variable, probably clock.tick
can magnify the diference.

3. Memory garbage collection kicks in. you can try to do a explicit
garbage collection after each M frames to see if max time is reduced.

4. At each frame you can try to print some stats to see if there
something unexpected, by example:

print len(dirty), other info that you think is relevant, maybe the
total dirty area 

claxo


Re: [pygame] New on the mailing list with a strange FPS behavior

2007-11-11 Thread Brian Fisher
On Nov 11, 2007 1:41 PM, Ghislain Leveque [EMAIL PROTECTED] wrote:
 Today I've come into a strange behavior with my rendering loop.

 I've measured the time to each call to this draw function and the
 normal time is 15ms (quite fast) but from time to time, the function
 takes 500ms to execute !! And of course I see my game freezing...

Are you 100% sure the problem is the rendering loop, and that it's not
something else causing the problem?

The reason I ask is you said your draw function usually takes 15ms...
if your target fps is 60, then that means drawing is taking more than
90% of your 16.6ms per frame budget. If there was some other external
element causing a delay by interrupting your program, it would
interrupt your draw 90% of the time, and would make it look like it
was the draw's fault.

One way to test this is to time all the parts of your game and log
them or draw them to a real time display or something. If you see the
500ms delay come in some other component, or you see your game freeze
but draw didn't go to 500ms, then you know that you are getting a long
delay that draw is not causing.

if that's the case, it's probably another app (this can happen with an
app that takes 100% cpu, which it sounds like yours does if draw takes
15ms) or some background thread or asynchronous process (I think
streaming audio with pygame can do it, or garbage collection like
claxo suggested). You can sometimes discover the offender with either
a live file access monitoring tool (try filemon if on windows) or by a
live cpu use monitor (just see if some other process jumps in every
few seconds)

If you are 100% sure it's in your draw... then you could maybe try
putting in code to time each draw, and do something special if it goes
long. You could try doing the draw again and time that to find out if
it's due to what  how you drew that particular frame or not. If a
slow draw is repeatable, then you could run with a debugger and step
through the bad draw. If it's not repeatable... well.. good luck to
you...


Re: [pygame] New on the mailing list with a strange FPS behavior

2007-11-11 Thread RR4CLB
Hi!

This seems to sound like either an anti-virus program running in the 
background or something like SKYPE that takes up CPU time and will lock up the 
computer at an unpredictable time. I think all other programs running in the 
background should be turned off...

Bruce

- Original Message - 
From: claxo [EMAIL PROTECTED]
To: pygame-users@seul.org
Sent: Sunday, November 11, 2007 9:33 PM
Subject: Re: [pygame] New on the mailing list with a strange FPS behavior




On 11 Nov 2007 at 22:41, Ghislain Leveque wrote:

 Hi there,
 
 I subscribed to the mailing list today because I've used pygame for a
 while now and I begin to be comfortable with it.

Wellcome!

 Today I've come into a strange behavior with my rendering loop.
 
 I use dirty rectangles to update the screen so my render function
 looks like this :
 
 def draw(self):
 self.spritegroup1.clear(self.window,self.background)
 self.spritegroup2.clear(self.window,self.background)
 # ...
 
 dirty = self.spritegroup1.draw(self.window) +
 self.spritegroup2.draw(self.window) # ...
 pygame.display.update(dirty)
 self.clock.tick (TARGET_FPS)
 
 I've measured the time to each call to this draw function and the
 normal time is 15ms (quite fast) but from time to time, the function
 takes 500ms to execute !! And of course I see my game freezing...
 
 Can anyone tell me where I should look at ?
 -- 
 Ghislain Lévêque


1. I don't known if clock.tick() can be to blame, maybe you can try 
to bypass clock.tick to see if times are irregular.

2. If the size or quantity of sprites have great variations, so can 
the time. And if the workload is too variable, probably clock.tick 
can magnify the diference.

3. Memory garbage collection kicks in. you can try to do a explicit 
garbage collection after each M frames to see if max time is reduced.

4. At each frame you can try to print some stats to see if there 
something unexpected, by example:

print len(dirty), other info that you think is relevant, maybe the 
total dirty area 

claxo





Re: [pygame] New on the mailing list with a strange FPS behavior

2007-11-11 Thread Ghislain Leveque
Hi all and thanks for your quick answers.

First I will explain how I did measure and what make me think it's the
draw that stucks :

I use a custom MVC model and the main game object sends a cpu_tick
event as quick as it can. I this cpu_tick loop, many things happen
(objects move, towers fire, lose life, ) and the screen is drawn.

In my controller object (the one which receives all the events and
send them back to objects, I've a function like this that is called on
every event received :

def post(self,event):
  for listener in self.listeners:
now = pygame.time.get_ticks()
listener.notify(event)
self.stats[listener].append((pygame.time.get_ticks() - now,event))

So, as you can see, I measure the time taken by each object to react
to each event and the freeze is really created by the draw function.

To be sure, I've added in my draw_function :
def draw(self):
  now = pygame.time.get_ticks()
  # clear stuff
  # draw stuff
  self.stats.append(pygame.time.get_ticks() - now)

And I can see the freezing.

This morning, I tried my game on Windows (I'm developping on Linux)
and, on Windows, the game is faster and don't freeze. So maybe RR4CLB
is right and I just have another program using some of my CPU on the
LInux box...

I'll investigate more



-- 
Ghislain Lévêque


Re: [pygame] New on the mailing list with a strange FPS behavior

2007-11-11 Thread Ghislain Leveque
2007/11/12, claxo [EMAIL PROTECTED]:
 On 11 Nov 2007 at 22:41, Ghislain Leveque wrote:
  Today I've come into a strange behavior with my rendering loop.

 3. Memory garbage collection kicks in. you can try to do a explicit
 garbage collection after each M frames to see if max time is reduced.

How do I do an explicite garbage collection ?

Thanks

-- 
Ghislain Lévêque