Help with Timer

Ok so I was thinking about going to Jogl or Slick or LWGL but I’ve spent so much time with Java2d that I thought that maybe I could optimize things to run well on large screen sizes over 1000 wide. With that in mind, I’ve started testing everything I can think of. The first thing weird that occurs is the timer.

I’m using the standard Kevin Glass setup with a delta with non-recommended System.currentTimeMillis

try { Thread.sleep(loopTime+10-System.currentTimeMillis()); } catch (Exception e) {}

Results

  1. Framerate changes drastically everytime I run the test even though all I’m doing is drawing tiles and backgrounds. Sometimes it is 65 fps and others 94.
  2. For some reason setting the delay between 9-12 seems to have little effect on the frame rates.

Question: Why does the frame rate vary everytime I run it?

Okay so now I throw out the System.currentTimeMillis and go to the Gage timer.

try { Thread.sleep(loopTime+10-SystemTimer.getTime()); } catch (Exception e) {}

Presto I start getting 95 to 96 fps everytime.

Question why would I be getting a 1% improvement in frame rates wth a different timer?

Is there a better timer than Gage?

Additional notes: I reran these test about 60 times and it really seems like Java has an issue with the 10 delay. Even with both timers I get different frame rates. 11 is fine and 9 is fine but 10 seems to cause problems with both of them.

System.nanoTime

I’m guessing you’re running a Windows machine, because the resolution of currentTimeMillis on Windows is really pretty bad - somewhere around 50 ms I think. This can cause a fluctuating framerate.

I don’t know why 10 specifically causes problems - I’d guess it’s actually caused by something else.

It’s usually between 15 and 17 for my, with some spikes sometimes…

Yes windows xp, sorry for not being clear.

I changed the code to: try { Thread.sleep(10); } catch (Exception e) {}

And got the same results. 10 is not liked by the system. With the Gage timer, both 9 and 11 are faster. With System.currentTimeMillis(); it is all over the place but always slow on 10 about 65 fps.

hmm I’ll look into the nanotimer.

What a mess! Seems to be a Microsoft / Java issue. So doesn’t really matter if you use nano or whatever.

-XX:+ForceTimeHighResolution kills performance but makes the resolution 10 always or something like that. I always get 65 fps never higher. This might be a good thing for animation if not so low. I wonder what it does on other operating systems?

here is the sun blog

http://blogs.sun.com/dholmes/entry/inside_the_hotspot_vm_clocks

David Holmes suggests a task repeater but that seems like it might be bad for games. Anyone tried it?

Hm, I rarely test on Windows personally, but this is definitely not the first time I’ve heard of problems with the Windows timer. On Mac OS X I’ve never had issues with either currentTimeMillis or nanoTime.