Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
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  
  Precise timer compatible Java 1.1  (Read 2884 times)
0 Members and 1 Guest are viewing this topic.
Offline DzzD
« Posted 2006-04-13 10:45:18 »

Hi, this is a try to do a more precise timer than System.currentTimeMillis

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
public class Timer implements Runnable
{
   private long lastTime;
   private long newTime;
   private long lastDiff;
   private long lastCount;
   private long count;
   private boolean run;
   private Thread process;
   
   public Timer()
   {
      this.lastCount=1;
      this.count=0;
      this.lastTime=System.currentTimeMillis();
      this.newTime=System.currentTimeMillis();
      this.process=new Thread(this);
      this.process.start();
      this.process.setPriority(Thread.MAX_PRIORITY);
   }
   
   public void start()
   {
   }
   
   public long getTime()
   {
      if(this.lastCount==0)
         return 0;
           
      long predicted=this.lastTime-this.lastDiff+this.count*this.lastDiff/this.lastCount;
     
      if(predicted<this.newTime)      
         return predicted;
      return this.newTime;
   }
   
   public void run()
   {      
      while(true)
      {
         this.setClock();
         try
         {
            Thread.sleep(1);  
         }
         catch(InterruptedException ie)
         {
            return;
         }
      }
   }
   
   private void setClock()
   {
      this.newTime=System.currentTimeMillis();        
     
      if(this.newTime>this.lastTime)
      {
         this.lastCount=this.count;
         this.count=1;
         this.lastDiff=this.newTime-this.lastTime;
         this.lastTime=this.newTime;
      }
      else        
      {
         this.count++;
      }        
   }
}


here a code to test

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
      this.t=new Timer();

      int nbLoop=100;
     
      long systemTimes[]=new long[nbLoop];
      long timerTimes[]=new long[nbLoop];
      long diffTimes[]=new long[nbLoop];
     
      long systemTime=System.currentTimeMillis();
      long timerTime=t.getTime();
     
      for(int n=0;n<nbLoop;n++)
      {
         
         try
         {
            Thread.sleep(1); //Replace that to try different interval
        }
         catch(InterruptedException ie)
         {
           
         }

         long newSystemTime=System.currentTimeMillis();
         long newTimerTime=t.getTime();
         systemTimes[n]=newSystemTime-systemTime;
         timerTimes[n]=newTimerTime-timerTime;
         diffTimes[n]=newSystemTime-newTimerTime;
         
         //timerTime=newTimerTime;        
        //systemTime=newSystemTime;
     }

      System.out.println("systemTimes[n]\t\ttimerTimes[n]\t\tdiffTimes[n]");
      for(int n=0;n<nbLoop;n++)
      {
         System.out.println(systemTimes[n]+"\t\t"+timerTimes[n]+"\t\t"+diffTimes[n]);            
      }  

Offline pepe

Junior Member




Nothing unreal exists


« Reply #1 - Posted 2006-04-13 14:21:02 »

Hello.
Nice one !
I first didn't think this would work, as sleep() ought to have the same granularity as currentTimeMillis() -or so i believed-.
Maybe there is a special case with (1)?

Did you make any profiling in a real app ?

Home page: http://frederic.barachant.com
------------------------------------------------------
GoSub: java2D gamechmark http://frederic.barachant.com/GoSub/GoSub.jnlp
Offline DzzD
« Reply #2 - Posted 2006-04-13 18:41:22 »

no, I did not really try it on real app, but I guess it wil have some trouble if CPU is used 100% for too long

Quote
Maybe there is a special case with (1)?

the sleep(1) in the run method could be replaced by any other instruction that do not use much cpu and time , I use sleep because it is the only instruction matching those requierment

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2006-04-13 20:00:31 »

a runnable timer? Why? What's in it that you can't get with 3-4 lines of code using System.nanoTime()?

btw. I think sleep(1) is low res and could sleep 1-20 ms actually on windows. Windows has timer resolution of 20 ms I think and sleep uses it. Maybe I'm wrong, I remember this from Killer Game Programming in Java.
Offline DzzD
« Reply #4 - Posted 2006-04-13 20:19:27 »

nanotime doesnt exist in java 1.1

as I explain above sleep(1) can be replaced by any other instruction that use not much cpu and time, please look more closer to see how this timer works, it compute an average tick between two different time of currentmillis

Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #5 - Posted 2006-05-07 13:25:24 »

previous -> next
old -> new
first -> last

You have abit of a mix-up in your naming  Wink
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #6 - Posted 2006-05-07 23:25:59 »

The resolution of currentTimeMillis is 50-55 msec on win9x, 10msec on 2k/xp and 1msec on linux/mac.

On win9x you can surely sleep less than 50-55msec (down to about 5msec iirc), but usually it will be pretty jumpy, because there is absolutely no guarantee that sleep will actually return the control at the desired time.

The idea to use a seperate sleep thread isnt new btw. Someone (from here I think) experimented with that a few years ago. Well, I personally prefer my adaptive yield loop thingy, which is just fine for most kind of games (if you're fine with the tickbased route) and it even works pretty well if the timer resolution is as bad as 250msec.

Trying to make a general purpose timer somehow misses the point a bit imo. If you only want to use it for games (or similar things) you can do things a bit differently. If its used in a render loop the time is requested once per frame and the delta from one frame to the next will be always pretty similar (compared to the previous one) for the most part.

That means that you can use things like rolling average, inter-/extrapolating  and damping for getting a pretty accurate delta.

For example if the last 5 frames took 10msec each (on average) and the calculated delta determined with currentTimeMillis is 0, then the real delta is actually most likely pretty close to 10msec instead of 0.

弾幕 ☆ @mahonnaiseblog
Offline DzzD
« Reply #7 - Posted 2006-05-08 09:26:24 »

I began this thread with that sentence

Quote
...this is a try to do a more precise timer ...

and so, I never said that this timer should be used for general purpose but in some case it will help to get a better timer on 1.1 platform target.

Quote
The idea to use a seperate sleep thread isnt new btw. Someone (from here I think) experimented with that a few years ago.


what this timer do is a bit more complex than : while(Thread.sleep(1)) time++ ;-)
as I explain above you can replace the Thread.sleep(1) by Thread.yield() it will works the same even with a system timer resolution as bad as 250ms

EDIT: this timer works like an FPS counter (reversed of course)

Quote
Well, I personally prefer my adaptive yield loop thingy, which is just fine for most kind of games (if you're fine with the tickbased route) and it even works pretty well if the timer resolution is as bad as 250msec.

it can also be used in a main loop without the need to start the thread by calling: setClock() in the main loop, i guess that it will works the same.

EDIT: if framerate is higher than the timer resolution

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.

pw (13 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!