|Benchmarking with System.nanoTime||System.currentTimeMillis Accuracy||Repeating Events|
|Pentium RDTSC||Measuring System.currentTimeMillis Accuracy||Learning More|
|Non-Java Schemes||Units of Time||Links|
|Performance Monitoring||Mixed Base Conversion|
For benchmarking, use System.nanoTime which has much greater resolution than System.currentTimeMillis, but is not synchronized to UTC (Coordinated Universal Time/Temps Universel Coordonné) time in any way. There is no guarantee of resolution. It is not even synchonised between different JVMs (Java Virtual Machines) running on the same machine! I have already heard that you can’t even trust it to be synchronised between threads. All you can count on is that is will not run backwards even when a thread runs on different cores. You may well find every value it returns ends in 000000 because the clock cannot provide better than millisecond resolution/granularity. On a 2 GHz machine, 1 nanosecond represents about two machine cycles or the time at takes to do two additions.
// 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;
On Windows boxes, a 1.5 JVM (Java Virtual Machine) uses the Win32 function QueryPerformanceCounter() which 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 cycles, use the assembler instruction RDTSC (Read Time Stamp Counter). 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 (Central Processing Units) 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 multiple 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. currentTime InMillis () or System. timeInMillis().
If you are stuck using a JDK (Java Development Kit) 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 (Java Native Interface) 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/W3.1/OS2/W95/W98/Me the dates were kept internally in local time.
|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|
|1 or 15.625 ms||Vista. 1 only if you sleep between calls to currentTimeMillis. I kid you not.|
|1 ms||Windows 7.|
|1 ms||Mac OS X|
Here is the source code for measuring the resolution of System.currentMillis on your own computer.
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.
System.nanoTime() returns the current value of the most precise available system timer, in nanoseconds. It is usually the CPU instruction clock, calibrated and converted into nanoseconds.
Imagine trying to explain this to a space faring species that used but a single unit of measure for time. Java 1.5 added the TimeUnit class to help convert between all these different ways of measuring it.
|Unit of Measure||Definition|
|millennium||1000 years, 10 centuries.|
|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, ¼ year. Interval between solstice and equinox.|
|quarter||approximately 3 months, ¼ year. Business term for an arbitrary accounting period.|
|month||28 to 31 days.|
|fortnight||2 weeks, 14 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.|
available on the web at:
optional Replicator mirror
Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : . If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Your face IP:[188.8.131.52]
|Feedback||You are visitor number 40,981.|