nanotime
How to convert epoch time with nanoseconds to human-readable?
First, convert it to a datetime object with second precision (floored, not rounded): >>> from datetime import datetime >>> dt = datetime.fromtimestamp(1360287003083988472 // 1000000000) >>> dt datetime.datetime(2013, 2, 7, 17, 30, 3) Then to make it human-readable, use the strftime() method on the object you get back: >>> s = dt.strftime(‘%Y-%m-%d %H:%M:%S’) >>> s ‘2013-02-07 … Read more
How to suspend a java thread for a small period of time, like 100 nanoseconds?
The granularity of sleeps is generally bound by the thread scheduler’s interrupt period. In Linux, this interrupt period is generally 1ms in recent kernels. In Windows, the scheduler’s interrupt period is normally around 10 or 15 milliseconds If I have to halt threads for periods less than this, I normally use a busy wait EDIT: … Read more
Conversion of nanoseconds to milliseconds and nanoseconds < 999999 in Java
For an ever shorter conversion using java.util.concurrent.TimeUnit, equivalent to what Shawn wrote above, you can use: long durationInMs = TimeUnit.NANOSECONDS.toMillis(delayNS);
Java 8 Instant.now() with nanosecond resolution?
You can consider yourself lucky if you get even millisecond resolution. Instant may model the time to nanosecond precision, but as for the actual resolution, it depends on the underlying OS implementation. On Windows, for example, the resolution is pretty low, on the order of 10 ms. Compare this with System.nanoTime(), which gives resolution in … Read more
Is System.nanoTime() completely useless?
This answer was written in 2011 from the point of view of what the Sun JDK of the time running on operating systems of the time actually did. That was a long time ago! leventov’s answer offers a more up-to-date perspective. That post is wrong, and nanoTime is safe. There’s a comment on the post … Read more