Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
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  
  Timing  (Read 3891 times)
0 Members and 1 Guest are viewing this topic.
Offline Sanxion

Junior Devvie




Java games rock!


« Posted 2003-03-16 14:49:03 »

How reliable is System.currentTimeMillis()? Is it possible that the currentTimeMillis() starts falling behind if the computer has a heavy workload? I'm using it to time frames per second and it seems to behave oddly sometimes...
Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #1 - Posted 2003-03-16 14:59:03 »

currentTimeMillis may have a crap timer resolution,
but it shouldn't suffer from drift.

how are u calcing your fps?

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Sanxion

Junior Devvie




Java games rock!


« Reply #2 - Posted 2003-03-17 03:06:23 »

Here's the code. Let me know if it doesn't make sense.

     private static boolean   engineRunning = true;
     private static long startTime;
     private static int  SLEEP_TIME= 0;
     private static final int INTERVAL= 25;
     public static int CURR_FRAME = 0;

     public void run () {
           startTime=System.currentTimeMillis();
           while (engineRunning) {
                 SLEEP_TIME=(int) (startTime+(CURR_FRAME*INTERVAL)-System.currentTimeMillis());

                 if (SLEEP_TIME<0){
                       // scheduler is behind! This will cause huge problems!
                       System.out.println("Scheduler is behind:"+SLEEP_TIME+" ms. ("+(System.currentTimeMillis()-startTime)+")");      
                 }else{
                       try {sleep(SLEEP_TIME);}
                       catch(InterruptedException e) {}
                 }

                 // new frame
                 CURR_FRAME++;

                 // process events of frame etc. etc.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline leknor

Junior Devvie




ROCK!!!


« Reply #3 - Posted 2003-03-17 05:44:21 »

Different platforms have different resolutions for System.currentTimeMillis(). You're INTERVAL of 25 milliseconds maybe small enough that you're getting the same value twice.

Also Thread.sleep() is not guarenteed to start the instant it's time is up. It's possible that the operating system scheduler and/or the Java scheduler may decide to do other things before it gets around to waking up your thread once it's ready. Under heavy load this will be more likely. Also your rendering work could be doing too much work to do in one interval.

You can: a) not worry about it, b) skip frames, or c) reduce the amount of work by lowering the quality when you're behind.
Offline Sanxion

Junior Devvie




Java games rock!


« Reply #4 - Posted 2003-03-17 06:04:42 »

Thanks for the input. I've also noticed that thread.sleep() can +-5 ms. Is this the conventional way to code a Scheduler or are there better alternatives for keeping track of frames?
Offline Sanxion

Junior Devvie




Java games rock!


« Reply #5 - Posted 2003-03-18 17:07:34 »

I guess so.  Grin
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #6 - Posted 2003-03-18 18:50:22 »

What works the best (it seems) is to just generate as many frames per second as possible and let the VSync smooth everything. Of course, this is a problem if you can't push that many FPS for some reason. I just wrote a timer to help with this problem. Check out the discussions at:

http://www.JavaGaming.org/cgi-bin/JGOForums/YaBB.cgi?board=share;action=display;num=1048008212

and

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

Java Game Console Project
Last Journal Entry: 12/17/04
Offline coilcore

Senior Newbie




Java games rock!


« Reply #7 - Posted 2003-03-18 22:32:40 »

System.currentTimeMillis() most likely uses the Real Time Clock (RTC) which is often not handled by the CPU itself but a daughter component.  This is NOT the same as the systems internal clock cycles which are susceptible to skew based on load.  

Intel platforms typically use a RTC that has a 19ms interval, so things which look use this will not show any difference between System.currentTimeMillis() calls within 19ms.  That is to say that if you time two events that take less than 19ms, it is likely you will show zero time used.  





Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #8 - Posted 2003-03-19 12:37:03 »

Partially correct. The Intel timer chip beats about once every 19.2 ms (as you said) and is used to keep the system clock running. The one trick here is that the chip can be reprogrammed. Every DOS video game I ever knew of reset it to align with the VSync. And modern day OSes reset the clock to whatever the heck they feel like. Under Windows 98 it's something like 50 ms, while Windows 2000 & XP set it to 10 ms. The *nixes don't screw around. They tick every millisecond. Try downloading the timer code I wrote and you'll be able to measure the timer resolution for various systems.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline darcone

Junior Devvie




Size matters


« Reply #9 - Posted 2003-04-01 08:23:11 »

If you want reliable timing use the LWJGL Sys.getTime() which has a really nice resolution Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline darcone

Junior Devvie




Size matters


« Reply #10 - Posted 2003-04-01 08:24:19 »

Oh yeah.. another thing.. if you really need to use the System.currentTimeMillis(), you really should capture 10-12 frames and divide the result by 12 to get a average time.
Offline Sanxion

Junior Devvie




Java games rock!


« Reply #11 - Posted 2003-06-29 09:41:30 »

Thanks again for all the input. I've been away for awhile, but now starting looking at this again.

I ran the code I pasted above on two machines next to each other. Within minutes they are off from each in seconds!!

I figure from some of the advice I got that some believed I was coding a client, which is not the case. Wink This is a server and it gets terrible when the clients and server get way out of sync, which is my problem.

Should I really be using System.currentTimeMillis() on the server? It behaves very differently between just two machines.... I'm getting differences beyond 10-50 ms, they now end up 1000's ms off each other with time.

Thanks for your input.
Offline Sanxion

Junior Devvie




Java games rock!


« Reply #12 - Posted 2003-06-29 09:57:53 »

Btw is there a way to analyze the system clock on a computer and get it tuned? I got no way of telling if the clock is healthy...
Offline aikarele

Senior Newbie





« Reply #13 - Posted 2003-06-29 19:47:44 »

Now that J2SE 1.4.2 is officially released, I would suggest you use the new high resolution timer it provides. Read more about it here:
http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=Tuning;action=display;num=1053157119
Offline kevglass

« JGO Spiffy Duke »


Medals: 199
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #14 - Posted 2003-06-29 19:50:32 »

Is the "Perf" class actually part of the officially exposed API now, or is it still hidden?

Kev

Offline leknor

Junior Devvie




ROCK!!!


« Reply #15 - Posted 2003-06-30 02:41:06 »

Quote
Btw is there a way to analyze the system clock on a computer and get it tuned? I got no way of telling if the clock is healthy...
Yes, http://www.ntp.org/ . Seems they got a new site recently.

We run ntp on all our severs at work (Linux or AIX) that way we know my servers have correct time and we can effectively compare logs. I don't know about Windows but on Linux/Unix NTP figures out clock drift and will make adjustments and it makes incremental corrections. Such that if your clock is only a few minutes off it will effectively speed up or slow down the clock until it is in sync again. That way you will never have a jump forward or backwards in time.
Offline Jeff

JGO Coder




Got any cats?


« Reply #16 - Posted 2003-06-30 02:41:29 »

Anything in com.sun.* is, by definition, specific to the sun VM (and anyone who copies it) but not part of the standard.

I have every hope though that this functionality will move over to be part of the standard by 1.5

JK

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Mark Thornton

Senior Devvie





« Reply #17 - Posted 2003-06-30 08:31:24 »

Quote
Now that J2SE 1.4.2 is officially released, I would suggest you use the new high resolution timer it provides.

The original query appears to relate to synchronization between two machines. The high resolution timer is not guaranteed to be synchronized at all.

The NTP clients in Windows 2000 and XP work much the same as the *nix equivalents, although by default they reference a Microsoft time server which may not be very local (>100ms round trip delay from here).
Offline Sanxion

Junior Devvie




Java games rock!


« Reply #18 - Posted 2003-06-30 20:35:00 »

aikarele, I tried the code you submitted that uses sun.misc.Perf

The hiResTimer.currentTimeMillis() method goes haywire on one of my computers. It ranges from -540000 to 946000 making huge leaps between ticks. I'm not sure if it proves the computer is sick, but lets say it is... should I be changing the whole motherboard or is there a simple way to change the hardware clock...
Offline Ruru

Junior Newbie




Love the world, but use protection....


« Reply #19 - Posted 2003-07-10 21:53:37 »

The timers in windows based systems are not accurate and other background processes will cause inaccuracies.

My experience of this is in profiling my Prolog program. (using SCI Prolog FYI)The docs say that you will never get super accurate results because windows does NOT count actual CPU cycles devoted to a process. So the timings a estimates at best. (Making sure all your background processes are switched off will help)
Linux, of course, will return very accurate results.
This explains why Jeff is getting good results, you wont/can't on your windows systems.

This is very frustrating in my case because I will be using profiling to estimate execution times on a 600 machine cluster that has a mixture of XP and Linux boxes. Bugger.  Smiley

--
More than likely I have offended you with the above post. Don't feel bad, we are often upset by things we are too damned stupid to understand.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #20 - Posted 2003-07-11 00:29:14 »

Quote
The timers in windows based systems are not accurate and other background processes will cause inaccuracies.
...
The docs say that you will never get super accurate results because windows does NOT count actual CPU cycles devoted to a process. So the timings a estimates at best. (Making sure all your background processes are switched off will help)


Background processes have no effect on hardware timers, I think you are confused.  At worst the timer would lag if hardware interrupts were missed because of some other high priority code locking out the ISR for too long.

Remember we are measuring REAL time, not the amount of time that our process gets the CPU.

Offline Mark Thornton

Senior Devvie





« Reply #21 - Posted 2003-07-11 06:22:44 »

Quote
My experience of this is in profiling my Prolog program. (using SCI Prolog FYI)The docs say that you will never get super accurate results because windows does NOT count actual CPU cycles devoted to a process.


Windows NT, 2000, XP do count CPU time for each thread and process. Windows 9X and ME do not. The resolution is limited to that of the system clock (usually 10ms on single processor machines and 15.625ms on multiprocessors).

However as someone else has pointed out real time rather than CPU time is the subject of this thread.
Offline Mark Thornton

Senior Devvie





« Reply #22 - Posted 2003-07-11 06:37:40 »

Quote

Background processes have no effect on hardware timers, I think you are confused.  At worst the timer would lag if hardware interrupts were missed because of some other high priority code locking out the ISR for too long.


Windows system time is initialised from the hardware clock at boot but subsequently runs independently. It can and does drift if interrupts are missed (these days rather rare). It has also been found to drift if the clock period is changed too often (a side effect of using Thread.sleep(1) --- there is a bug in the bug parade relating to this. http://developer.java.sun.com/developer/bugParade/bugs/4500388.html

This drift may be corrected if an NTP client is in use.
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.

Gibbo3771 (9 views)
2014-11-24 19:59:16

trollwarrior1 (35 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!