views:

100

answers:

1

We have a few processes in Java that use nanoTime for internal benchmarking (since that is supposed to be accurate within the same thread).

The processes can communicate between themselves.

Is there a standard way of determining the difference in nanoTime between the threads? (e.g., mimic NTP?). Should this be done regularly?

+2  A: 

The javadoc qualifies the nanoTime() method with a lot of warnings. The gist of it seems to be that you can use it within a block of code to measure elapsed time, giving you "nanosecond precision, but not necessarily nanosecond accuracy".

That's wooly language, probably deliberately so. I would probably avoid comparing two different elapsed time calculations made using nanoTime(), it's likely to give you bad results.

If this is for benchmarking, it may be a better idea to benchmark the same code in a loop, bringing the elapsed time up into the millisecond range, allowing you to use currentTimeMillis() instead, which is more reliable.

skaffman