Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Is this a valid FPS calculation?  (Read 2720 times)
0 Members and 1 Guest are viewing this topic.
zahl2001
Guest
« Posted 2004-06-18 17:40:24 »

Since FPS is so important, I want to make sure I didn't make some sort of typical newbie programmer mistake here. If so, please straighten me out.

private void displayFrameRate(GL gl)
{
   // num frames since last update          
   frameCount++;

   // time passed since last update
   timeDelta = System.currentTimeMillis() - oldTime;

   // if it's been longer than a quarter second, update    
   if(timeDelta >= 250)                            
   {    
       // calculate current fps                                      
       fps = (frameCount * 1000.0f) / timeDelta;  
     
       // oldTime becomes the current time
       oldTime = System.currentTimeMillis();        

       // reset frame count                              
       frameCount = 0;

       //...                               
   }
}      

Thanks Smiley
Offline kevglass

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2004-06-18 18:08:06 »

On windows in <1.5 this might not give good results because of the timer resolution of System.currentTimeMillis() being poor on some systems.

Seems ok other than that tho,

Kev

zahl2001
Guest
« Reply #2 - Posted 2004-06-18 18:55:22 »

kevglass

I have heard that 1.5 has a higher rez timer, but I am not familiar with how to use it. Could you recommend a modification to this code?

Thanks for the advice.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #3 - Posted 2004-06-19 01:34:17 »

My suggested improvements:

1) Use integer math to control the rounding errors better. (I use "timeDelta = timeDelta%msPerFpsSample" to let the "rounding error" spill over to the next iteration)

2) Don't sample the time twice. Sample it once ("long now = System.currentTimeMillis()") and reuse the value.
Sampling it twice means the time between the two System.currentTimeMillis() will go uncounted, resulting in an fps calculation that is slightly too fast.

3) Don't use System.currentTimeMillis. Wink
If you have to, set fpsSamplesPerSecond to a value as low as possible.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
int fpsSamplesPerSecond = 4;
int msPerFpsSample = 1000/fpsSamplesPerSecond;
int frameCount;
int timeDelta;

private void displayFrameRate(GL gl)
{
  frameCount++;
 
  long now = System.currentTimeMillis();
  timeDelta += (int)(now - oldTime);
  oldTime = now;
 
  if(timeDelta >= msPerFpsSample)
  {
    int passedSamples = timeDelta/msPerFpsSample;
    fps = frameCount * fpsSamplesPerSecond / passedSamples;
    timeDelta = timeDelta%msPerFpsSample;
    frameCount = 0;  
  }
}



[edit: Oops.]

[edit again: Made it handle when timeDelta > msPerFpsSample*2]

Play Minecraft!
zahl2001
Guest
« Reply #4 - Posted 2004-06-19 10:39:52 »

Markus,

Your version is clearly superior to mine, so I will happily incorporate it into the base code I am slowly getting together for my learning projects.

Quote

Don't use System.currentTimeMillis.  
If you have to, set fpsSamplesPerSecond to a value as low as possible.

I had a question about this: it seems you are suggesting I might remove the need for the call to System.currentTimeMillis() by altering the value of fpsSamplesPerSecond, and, if so, I don't understand how to do it and why it would work.

Thanks Smiley
Offline GKW

Senior Member




Revenge is mine!


« Reply #5 - Posted 2004-06-19 11:09:42 »

The high resolution timer in 1.5 is called with System.nanoTime().
zahl2001
Guest
« Reply #6 - Posted 2004-06-19 11:12:57 »

What's the granularity on Windows XP?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2004-06-19 11:29:29 »

It should be the same as Win2k, so in the range of 10ms.

Edit: stupid me. 10ms is the resolution of System.getTime on XP. An actual hi res timer on XP is rather accurate (down to clock ticks if I remember right).

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
zahl2001
Guest
« Reply #8 - Posted 2004-06-19 11:55:20 »

So, until Uncle Bill improves his OS, will there be any advantage to using nanoTime?
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #9 - Posted 2004-06-19 12:07:45 »

Quote
I had a question about this: it seems you are suggesting I might remove the need for the call to System.currentTimeMillis() by altering the value of fpsSamplesPerSecond, and, if so, I don't understand how to do it and why it would work.

Thanks Smiley


No, you're always going to have to call some kind of timer. =)
I just meant that if you're stuck with the only one included in standard java 1.4 (ie System.currentTimeMillis()), you have to keep in mind it's fairly inaccurate at high sample rates.

Play Minecraft!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
zahl2001
Guest
« Reply #10 - Posted 2004-06-19 12:13:27 »

Well, that's a relief...I thought you people must REALLY be gods if you had some trickery to get around calling the timer Smiley

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #11 - Posted 2004-06-19 13:03:28 »

Quote
So, until Uncle Bill improves his OS, will there be any advantage to using nanoTime?


Well, on win9x it's even worse. 50-55msec.

The advantage you ask? It's precision Smiley

The usual (old) timers aren't even good enough for measuring network latency. They are good for nothing. Whereas a nano timer can give you a very precise idea of how much time actually passed. You can use it for frame capping or accurate time based movement without using all those ugly hacks.

So if you can use a nano timer - use it.

弾幕 ☆ @mahonnaiseblog
zahl2001
Guest
« Reply #12 - Posted 2004-06-19 14:18:37 »

Ok, I saw Orangy Tang's edit to his post, and the timer deal makes sense now.
zahl2001
Guest
« Reply #13 - Posted 2004-06-19 17:02:15 »

I just implemented the nano timer for use in elapsed-time based adjustments to player position etc. and it was certainly a night and day change in performance.

Thanks for the tip.
Offline ribot

Junior Member




Ribot - mobile UI specialist


« Reply #14 - Posted 2004-06-19 17:11:54 »

What would be the disadvantages of integrating the Gage timer (other than a slightly increased total application size)?

http://ribot.co.uk - design agency focused on mobile
http://www.retrospecs.co.uk - online vintage eyewear store
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #15 - Posted 2004-06-19 17:33:30 »

We use the GAGE timer in wurm so we'll get good timing in java 1.4.
Haven't noticed any disadvantages.


Oh, well, actually, since it's native code, you'll need to sign your application. By using the 1.5 timer, you could make a "safe" game that doesn't require signing.

Play Minecraft!
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #16 - Posted 2004-06-19 21:08:14 »

Well, jogl is also an evil native thingy. So he has to sign it anyways Roll Eyes

弾幕 ☆ @mahonnaiseblog
Offline ribot

Junior Member




Ribot - mobile UI specialist


« Reply #17 - Posted 2004-06-19 21:21:40 »

evil. <evil laugh>ha ha ha</evil laugh>

I don't want to have to focus my clients to use 1.5 (which isn't officially out yet - when will it be?), so i'll look into implementing the Gage timer tomorrow which should take all of 3 minutes if that to integrate.  Cheesy

http://ribot.co.uk - design agency focused on mobile
http://www.retrospecs.co.uk - online vintage eyewear store
Offline Markus_Persson

JGO Wizard


Medals: 14
Projects: 19


Mojang Specifications


« Reply #18 - Posted 2004-06-19 21:28:06 »

Quote
Well, jogl is also an evil native thingy. So he has to sign it anyways Roll Eyes


I was just listing disadvantages of using gage timer. Wink
If you're already signing your app, that disadvantage is fairly small.

Play Minecraft!
Offline ribot

Junior Member




Ribot - mobile UI specialist


« Reply #19 - Posted 2004-06-19 21:48:03 »

ok then, so it's good news all round! I'll implement Gage tomorrow - easy to implement, nice compliant high res timer, low footprint = good good good!

I found the following interesting discussion on Gage timer implementation with OSX:

http://forum.java.sun.com/thread.jsp?forum=406&thread=511289&tstart=120&trange=15

http://ribot.co.uk - design agency focused on mobile
http://www.retrospecs.co.uk - online vintage eyewear store
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #20 - Posted 2004-06-19 22:01:08 »

Take a look at Kev's neat tutorials. Especially this one:
http://grexengine.com/sections/externalgames/articles/Kevin%20Glass-Space%20Invaders%20101-2.html

He also explains the differences between the timers n stuff. Well, take a look Smiley

弾幕 ☆ @mahonnaiseblog
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (10 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (27 views)
2014-08-22 19:31:30

atombrot (40 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (34 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!