File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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: http://snippets.hoydaa.org/simple-stopwatch-to-calculate-actual-thread-time
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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
Rancher

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

Bill
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Also read UseAMeaningfulSubjectLine


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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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: 14433
    
  23

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Who can explain this?