Home arrow static arrow Java Programming [Archive] - Is microsecond timing possible?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Is microsecond timing possible?
This topic has 7 replies on 1 page.

Posts:35
Registered: 5/21/03
Is microsecond timing possible?  
Jun 27, 2004 11:13 PM



 
Hi all,

I'm helping my friend with a real time simulator. The simulator has a time scaler in which 1us is the smallest time unit. So can I use the method "public static void sleep(long millis, int nanos)"? I read some documents saying that we can't get to nanos level in any OSes.

Any help is highly appreciated!

Thanks
 

Posts:2,909
Registered: 13.8.2003
Re: Is microsecond timing possible?  
Jun 27, 2004 11:45 PM (reply 1 of 7)



 
sleep(long millis, int nanos) isn't implemented correctly (basically it calls sleep(long millis)) so there's no way to get microsecond timing.

You can get 1 millisecond precision depending on your OS (*NIX and I think winXP give 1 millisecond, win2k gives you 10 millisecond and win9x gives you 50ms precision).
 

Posts:1,160
Registered: 7/24/97
Re: Is microsecond timing possible?  
Jun 28, 2004 1:01 AM (reply 2 of 7)



 
For such projects you are going to need a Real Time capable Java VM.

There is work in progress on Java as a real time environment, search the java.sun.com web site for information
 

Posts:334
Registered: 5/23/97
Re: Is microsecond timing possible?  
Jun 28, 2004 1:39 PM (reply 3 of 7)



 
See:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#nanoTime()

Now I wonder what the actual resolution of this is.
I'd write a test program but I don't have 1.5 on this machine; it interfered with my Motorola MIDP version.

As a guess I suspect it will be no better than 50MS accurate.

(T)
 

Posts:2,821
Registered: 7/10/01
Re: Is microsecond timing possible?  
Jun 28, 2004 1:51 PM (reply 4 of 7)



 
As the method defines - nanoTime uses the best timer available on the platform.

On windows, it uses the multimedia timer (aka QueryPerformanceCounter()) so the resolution is in the order of a few hundred nanoSeconds.
 

Posts:441
Registered: 2/25/04
Re: Is microsecond timing possible?  
Jun 28, 2004 4:27 PM (reply 5 of 7)



 
Don't know what OS you're on, but http://forum.java.sun.com/thread.jsp?forum=31&thread=475165&message=2202924#2202960
may help, if you're on windows and just want to synch. to a more accurate clock.

You can also replace the system function call with the asm to read the cpu's tick counter if you need anything more, but once you're back into Java the execution speed isn't guarenteed.

Pete

 

Posts:447
Registered: 3/8/01
Re: Is microsecond timing possible?  
Jun 28, 2004 5:04 PM (reply 6 of 7)



 
No matter which timer you use, you shouldn't use Thread.sleep for such small delays. Instead, use a tight while loop (while(timer()<nextTime) {;}).
 

Posts:35
Registered: 5/21/03
Re: Is microsecond timing possible?  
Jun 30, 2004 2:17 AM (reply 7 of 7)



 
Thanks for all of your replies guys! I've just told my friends that we can't get us timing in java and he agreed to use ms timing.
 
This topic has 7 replies on 1 page.