| Benchmarking | Timer Accuracy | Learning More |
| Pentium RDTSC | Units Of Time | Links |
| Non-Java Schemes | Mixed Base Conversion | |
| Performance Monitoring | Repeating Events |
// elapsed time with System.nanoTime // Works only in JDK 1.5+ long start = System.nanoTime(); // .. do something // elapsed time in nanoseconds, billionths of a second long elapsed = System.nanoTime() - start;which has much greater resolution than System.currentTimeMillis, but is not synchronized to UTC time in any way. There is no guarantee of resolution. You may well find every value it returns ends in 000 because the clock cannot provide better than millisecond resolution. On a 2 GHz machine, 1 nanosecond represents about two machine cycles or the time at takes to do two additions.
On Windows boxes, a 1.5 JVM uses the Win32 function QueryPerformanceCounter() which has has no defined resolution. It reports clock cycles since bootup. The JVM has to use QueryPerformanceFrequency() to interpret the results. The JVM does this for you and reports the results of System. nanoTime in true nanoseconds. If you want the finer resolution of clock cylcles, use RDTSC. This means that System. nanoTime results are useful both for comparing algorithms on the same hardware and for benchmarking different hardware on the same algorithm.
Unfortunately, if you have multiple CPUs each has its own nanotimer, and Windows does not properly keep them synchronised. So you hop back and forth between different timers as your task runs on different CPUs. Linux has ways of dealing with this. Power Management also causes drift between muliple CPUs.
If you have to code for 1.4-, then you can only get millisecond accuracy. You need to use:
// elapsed time with System.currentTimeMillis // Works in all JDK versions long start = System.currentTimeMillis(); // .. do something // elapsed time in microseconds, millionths of a second long elapsed = System.currentTimeMillis() - start;Watch the spelling of System.currentTimeMillis(). If you are like me you will be tempted to spell it as System. getCurrentTimeMillis() or System. currentTimeInMillis () or System. timeInMillis().
If you are stuck using a JDK 1.4-, then you could roll your own nanoTime method. RDTSC reads the cycle counter into EDX:EAX. Some operating systems may make this a privileged instruction. See Intel’s documentation for more details. You could access them via a bit of JNI with the Pentium class that I wrote and posted the code for. If you use that method, make sure you calibrate so you can discount the considerable overhead of the JNI calls themselves.
Microsoft timestamps files with 64 bit hundreds of nanoseconds since 1601-01-01 UTC Gregorian. I presume they use Pope Gregory’s flip date to avoid the complication of the missing days. The FileTimes package converts from Java time to Windows native time to access the create date and last access date of a Windows file. In DOS/Win 3.1/W95/W98/Me the dates were kept internally in local time.
| Timer Resolution | |
|---|---|
| Resolution | Platform |
| 55 ms | Windows 95/98 |
| 10 ms | Windows NT, Windows 2000, Windows XP single processor |
| 15.625 ms | Windows NT, Windows 2000, Windows XP dual processor |
In addition the Thread.sleep method will achieve delays of 1 ms on a single processor and 2 ms minimum on a dual processor.
You can also use the JVMPI (Java Virtual Machine Profiler Interface) for accurate timing.
Imagine trying to explain this to a space faring species that used but a single unit of measure for time.
| Unit Of Measure | Definition |
|---|---|
| millennium | 1000 years, 10 centuries. |
| century | 100 years. |
| leap year | the approximate time with respect to the sun, for the earth to revolve around the sun 366 days. |
| year | the approximate time with respect to the sun, for the earth to revolve around the sun. Sometimes 365 and sometimes 366 days. |
| season | approximately 3 months, 1/4 year. Interval between solstice and equinox. |
| quarter | approximately 3 months, 1/4 year. Business term for an arbitrary accounting period. |
| month | 28 to 31 days. |
| fortnight | 2 weeks, 14 days. |
| week | 7 days. |
| day | the approximate time for the earth to rotate on its axis. 24 hours. In civil time, the day may be 23 or 25 hours long during daylight saving switch over in jurisdictions that use a 60 minute daylight saving offset. Using 86,400,000 ms for a civil day will get you in trouble spanning daylight saving switch over if you use local time rather than pure UTC. |
| hour | 60 minutes, 1/24 of a day. |
| minute | 60 seconds, 1/60 hour, 1/1440 day. It can occasionally be 59 or 61 seconds long when leap seconds are inserted. Java ignores leap seconds, treating them as physical imperfections of the clocks that are adjusted, but not tracked. |
| second | 1000 milliseconds, 1/60 minute, 1/3600 hour, 1/86,400 day. |
| millisecond | 1000 microseconds, 10-3 seconds, 1/1000 second, 1/3,600,000 hour, 1/86,400,000 day. |
| microsecond | 1000 nanoseconds, 10-6 seconds, 1/1,000,000 second, 1/3,600,000,000 hour, 1/86,400,000,000 day. |
| nanosecond | 1000 picoseconds, 10-9 seconds, 1/1,000,000,000 second, 1/3,600,000,000,000 hour, 1/86,400,000,000,000 day. |
| picosecond | 1000 femtoseconds, 10-12 seconds, 1/1,000,000,000,000 second, 1/3,600,000,000,000,000 hour, 1/86,400,000,000,000 day. |
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.18] | ![]() | ||
| You are visitor number 43,870. | |||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/Mindprod website mirror) | ||
| http://mindprod.com/jgloss/time.html | J:\mindprod\jgloss\time.html | ||