The Artima Developer Community
Sponsored Link

Java Answers Forum
System time in nanoseconds

3 replies on 1 page. Most recent reply: Mar 21, 2002 5:44 AM by Lance Eason

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Paul Maguire

Posts: 1
Nickname: mag
Registered: Mar, 2002

System time in nanoseconds Posted: Mar 19, 2002 11:21 AM
Reply to this message Reply
Advertisement
Is it possible to get the system time in nanosecods or anything more precise than milliseconds?

Or is there a way to get amount of CPU time a particular method takes to run?

Thanks.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: System time in nanoseconds Posted: Mar 19, 2002 5:17 PM
Reply to this message Reply
Nanoseconds? What platform are you using? And can I have one? Even using the native multimedia APIs on Windows, such as timeGetTime(), you can only get down to milliseconds (this, of course, also depends on your hardware).

Using System.currentTimeMillis(), you aren't even getting that. I wrote a little test program and the results gave a granularity of 50 to 60 ms for it on my machine. That suspicious number (straddling 1/18 second) gives me the impression that the JVM is using the cruder GetTickCount() API call, which means it would never get better granularity, regardless of the hardware.

Although, it is possible that the 50-60 value is coincidental and it is really using timeGetTime(); then the rest of time is being lost in the JVM's overhead. However, I doubt there is that much overhead in a single call to a static method.

If you are on the Windows platform, you could write your own native method to call timeGetTime(). You will probably get better resolution than System.currentTimeMillis(), but worse than timeGetTime() itself, of course. If you are on Linux, or other platform, I'm not sure what the call is to get higher-resolution time intervals (you may need some assembly, though...)

Java isn't intended for realtime programming, you know. In particular, even if you did get good granularity of your time-calling method in one configuration, it would not necessarily be predictable or reliable. You never know when garbage collection could kick in, after all, not to mention the effects of other threads and processes.

As far as timing a method, you might want to search on google or "java profiler" and see what tools appear.

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: System time in nanoseconds Posted: Mar 20, 2002 12:29 AM
Reply to this message Reply
> Java isn't intended for realtime programming, you
> know. In particular, even if you did get good
> granularity of your time-calling method in one
> configuration, it would not necessarily be
> predictable or reliable. You never know when
> garbage collection could kick in, after all, not to
> mention the effects of other threads and processes.
>
Regular JVMs aren't suitable for real time applications, but there is a major effort to define a specialized JVM that is suitable for real time. James Gosling spent a good year helping with this effort back in 1999 and 2000. He talks about it here:

http://www.artima.com/intv/gosling22.html

The real time Java effort seems to me to be quite a community effort. More info is here:

http://www.rtj.org/

Lance Eason

Posts: 1
Nickname: leason
Registered: Mar, 2002

Re: System time in nanoseconds Posted: Mar 21, 2002 5:44 AM
Reply to this message Reply
The JDK itself gives you no way to get resolutions below a millisecond (and as pointed out in some cases not even that). However, there is an open source graphics library, OpenPTC, which gives you access to clocks with whatever resolution is supported by the underlying OS. I can attest that on Windows NT I was getting resolutions significantly under a microsecond though I don't remember if it went all the way down to a nanosecond or not. The distribution is available at: http://www.gaffer.org/ptc/distributions/Java/

Flat View: This topic has 3 replies on 1 page
Topic: How to call a method inside a class in a Jar file Previous Topic   Next Topic Topic: EJB ,XML,JSP,Servlets

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use