Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: System time in nanoseconds
|
Posted: Mar 19, 2002 5:17 PM
|
|
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.
|
|