File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Who can explain this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Who can explain this?" Watch "Who can explain this?" New topic

Who can explain this?

Stephane Clinckart
Ranch Hand

Joined: Oct 21, 2003
Posts: 89
Who can expalin this ???

Resolution : 15600100ns - 15ms
Resolution Nano System: 810029ns - 0ms

"Resolution" should be lower than "Resolution Nano System" but it is not the case !

Thanks for help.

PS: The original code is from this blog:
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Looking at the values of those four numbers, instead of just looking at the difference between pairs of them, might illuminate the question. Provided you can replicate the phenomenon reliably, that is.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Just read the JavaDocs for java.lang.System and you will see that nanoTime has lots of limitations.

Darryl Burke

Joined: May 03, 2008
Posts: 5115

Also read UseAMeaningfulSubjectLine

luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46344
Last time I used nanoTime() a lot, it appeared to show the execution time to the nearest ~0.4µs. That will obviously differ from machine to machine.
Stephane Clinckart
Ranch Hand

Joined: Oct 21, 2003
Posts: 89
I'm not sure that I explain well.

What I don't understand is that Sustem.getNano is arround the other way to get the duration.

It looks like both methods are not using the same time referencial internaly.

start1 = getTime()
start2 = getCPUTime()

stop2 = getCPUTime()
stop1 = getTime()

Normally (stop1-start1) should be higger than (stop2-start2)

But it is not the case.

Even if I try in a loop.

Any idea what cause this ???
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Those timers are not very precise; to start with, their resolution depends on the operating system you're running on and can be as high as 50 ms or so. I also remember reading somewhere that under some circumstances the clock could even seem to be running backwards in time from one moment to the next.

Don't count on these timers to measure very short amounts of time very precisely.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
jQuery in Action, 3rd edition
subject: Who can explain this?
It's not a secret anymore!