A weird fps tale

Ok here’s the thing. I was testing my game at 50 frames per second. As you know the delay for 50 fps is 20 ms. I tried it out and all I could get was 32 fps.

I thought it was my game, so I stripped it out of “suspicious” methods and classes, but nothing. I ended up making a little program to test this fps situation.

The result was to say the least crazy. 20ms delay produces in reality a 31 or 32 ms delay. 19ms or 21ms (or anything else) delays are almost okay. Check out the code:


public class EmptyGame
{ 
   public static void main( String[] args ) 
   { 
     EmptyGame test = new EmptyGame(); 
   } 

  // A delay of 20L should give us 50 fps (1000 / 20)
  private static final long DELAY = 20L; // try either 19L or 21L to see the difference

  public EmptyGame() 
  {
    long startTime;
    int frames = 0; 
    long fpsTime = System.currentTimeMillis();

    while( true ) 
    { 
      startTime = System.currentTimeMillis();      
      frames++;
      if (startTime - fpsTime > 999)
      { 
            System.out.println("FPS: " + frames);
        frames = 0; 
        fpsTime = startTime; 
      }
        try
        {
          Thread.sleep( DELAY );
      } catch(Exception e) {}

      // Un-comment the following line to see the actual delay
      //System.out.print(System.currentTimeMillis()- startTime+"  ");
    } 
  } 
}

Please try it out and see for yourself. Is there something that I’m not aware of? Another Java bug perhaps?

Thanks.

Welcome to the wild and wooly world of system timing. You have two options here:

  1. Loop around a “sleep(5)” method until the time expires.
  2. Download GAGETimer and turn off the CPU monitor. (It’s just a psychological thing anyway…)

Thanks. I wish I knew what you meant by

[quote]Loop around a “sleep(5)” method until the time expires.
[/quote]
Is it about the 5ms threshold (or something) of the java timer?

Update: No need to reply. Got it now, thanks.

[quote]Thanks. I wish I knew what you meant by
Is it about the 5ms threshold (or something) of the java timer?
[/quote]
The resolution of System.currentTimeMillis() under win9X is 50-55msec… however the accuracy of sleep is actually 5msecs.

[quote]2. Download GAGETimer and turn off the CPU monitor.
[/quote]
I’ve come upon the same realization recently.
Could you please explain the bit about turning off the CPU monitor? How much would it really help and what’s the worst that could happen by doing so? Thanks.

I think JBanes was getting at that if you have the CPU Monitor (the system tool that comes as part of windows) turned on you’ll notice that with the GAGETimer in your loop you’ll see 100% CPU usage most of the time.

However, this is misleading in that the 100% doesn’t mean that nothing else can run, since should anything else attempt to run your game (based on the gage timer) will yield its processor time. Lots of people see the 100% usage and fear the worse but its simply a non issue.

Kev

Correct go buy UO and start it and look at your performance monitor and look its 100% and this game is from EA, one of the leading software companies, they know what they do trust me

People telling me 100% is bad are no serious programmers for me.

WOW nice got an extra DUKE

Well when a game is running it would ideally be using as much of the native platform as it could to achieve effect. No point running at 30% CPU and having hitches and pauses :slight_smile: