Štěpán Roh

Alive But Sleepy

@stepanroh
← shttpd/0.3.1 - give credit where credit is due Navigace Garmin a Linux →
Thursday, July 17, 2008

Unreliable System.nanoTime()

by Štěpán Roh

Java's System.nanoTime() is better than System.currentTimeMillis(), right? Wrong!

There is a subtle price paid for better accuracy of System.nanoTime() - on various platforms its value is taken from various sources. These sources may sometimes fail. And if they fail, garbage gets returned (most probably zero, but who knows for sure). This may seem like a very rare situation, but combination of hypervisor Xen and Windows produced garbage too much for our liking. Maybe it was caused by Xen bug #1146.

So if you measure something with System.nanoTime(), check whether the measurement is bigger than zero and (as we also did) whether it does not differ too much (like 1 second) from the measurement done using the good old System.currentTimeMillis().

← shttpd/0.3.1 - give credit where credit is due ↑Back to top Navigace Garmin a Linux →