Measure Time in Nano-Seconds



Since computers are getting faster and faster it might be insufficient to measure the time a certain task takes using only milli-seconds.
Since Java 5, the System class has a new method, nanoTime, that returns a nanosecond-resolution counter. You canӴ use it for measuring absolute time, but it works great for measuring time differences.

The example below shows how to use it:

Unfortunately, we have no guarantee that we will actually get nano-second measurements when we run this code.
But with a faster machine and a good JRE implementation, it’s a useful measurement for testing purposes.
You can find more information on this method in the JDK 5 documentation. Because of operating system characteristics, machine processing speed, and system load, you may get a wide variation in the values returned by the nanoTime method.

This is what the output was when run on the Javadb.com machine :)







Search for more Java info on this site here:
Custom Search


Any thoughts or questions?