I\'m encountering a really unusual issue here. It seems that the calling of Thread.sleep(n), where n > 0 would cause the following System.nanoTime() calls to be less predictable
I can suggest at least two possible reasons of such behavior:
Thread.sleep
it is likely to fall into one of power-saving states, with frequency and voltage reduced. After than CPU won't return to its maximum performance immediately, this may take from several nanoseconds to microseconds.Thread.sleep
, it will be scheduled for execution again after a timer event which might be related to the timer used for System.nanoTime
.In both cases you can't directly work around this - I mean Thread.sleep
will also affect timings in your real application. But if the amount of useful work measured is large enough, the inaccuracy will be negligible.