High-res, pure Java timer?

I’m sure I’m repeating a question that’s been asked many a time before…please bear with me (or, just go on to the next topic. :slight_smile:

Has a high-res, pure-Java timer come into existence yet? Are we still waiting for Tiger to release before we’ve got this?

Is the one in Tiger really going to deliver?

According to some SUN guys here in the forum there is a high res timer in Java 1.5 (please use search for details).

Until then you could use the unofficial Java 1.4.2 (and above) high res timer class. It’s named differently to the official 1.5 one but basically does the same (please use search for more details, too).


sun.misc.Perf myPerf    = sun.misc.Perf.getPerf();
long          frequency = myPerf.highResFrequency(); // How many ticks per second.

// ...

long  currentTicks  = myPerf.highResCounter();
float timeInSeconds = ((float) currentTicks) / frequency;
long  timeInMillis  = currentTicks * 1000 / frequency;

[Edited accuracy statement and float division]

it actually goes in the mycro second range.

counter() and frequency() return integers, if you divide both you still get an integer, so you cant get below 1 ms that way. but if you cast one to a floating point before the division you get much better resolution.

Its the same high res counter i used 20 years ago with assembly code, no problem for ppl who did this 20 years ago, they know how to deal with it.

if you are new to the gaming sector, it helps if u read some good books and get the basics.

its ok if a newbie ask a question but if you ask simply questions which can be answered easy by google or the search function, i sometimes think ppl are too lazy.

I think this is a very reasonable question though the guys who say you can find all the answers with a search on this BBS are correct and in general its something you should probably try first as there is a LOT of collected wisdom on these boards already.

Short:

(1) Yes with 1.5 its officially part of the standard Java APIs.
(2) It returns a resolution in nanoseconds. (Thats resolution, NOT accuracy.)
(3) It must return the best accuracy possible on the platform in question at the time it was called. If you can demonstrate that there is a way to get better accuracy then it is returning, thats a file-able bug.

That answer your questions?

[quote]it actually goes in the mycro second range.

counter() and frequency() return integers, if you divide both you still get an integer, so you cant get below 1 ms that way.
[/quote]
Yes, that’s right. I typed the code line using a float value just out of memory and missed the int division issue…

In a real world app I prefer ints/longs over floats (where rational) and also milliseconds are enough for me currently so actually I use the code line which has been uncommented: long timeInMillis = currentTicks * 1000 / frequency

[quote]its ok if a newbie ask a question but if you ask simply questions which can be answered easy by google or the search function, i sometimes think ppl are too lazy.
[/quote]
Using the search function is usually a good idea, that’s true.

However with Google and other search engines you’re soon lost in hyperspace. In particular when it comes to such a non-mainstream topic like “Java and gaming”.

Since this forum isn’t used by millions of people the traffic is well reviewable and the athmosphere rather familiar. That’s very nice. However this also means that oftenly threads handle several topics at once or go totally off-topic. In short: the essence of many searched threads is fragmented and not always easy to find…

Usually a short and simple answer helps the asker as well as the responder. (There’s a proverb saying you learn most by explaining.)

I have sdk 1.4.2 and I haven’t been able to locate the timer. The compiler tells me ‘unable to locate class etc’. Is it in a newer version of 1.4.2?

Whose 1.4.2 on what platform???

The only highres timer in 1.4.2 is in the sun specific libs on some platforms (don’t ask me which, i don’;t know.)

NOTHING thats starts with "com.sun." is standard. It is NOT gauranteed to be there in a 1.4.2 platform. Sun is not obligated to retain it between versions or have it on all platforms. com.sun. is what Sun uses internally for our implementation code.

As I said above the only place in the spec you are guaranteed a high res timer is in 1.5 where it will appear somewhere in the java.* or javax.* classes.

Okay, that clear?

Having said that, some people on some platforms in some versions of Sun’s JDK1.4 have found a high res timer buried in the com.sun.* classes. If, given all those issues, you still wish to use it I’m sure they can tell you where they found it.