• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem in time calculation.

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a method in which i have to calcute the time take for a function to execute.
here is the way i am trying to get the time.

-1 is getting displayed as the result. in which scenario this can happen ? as System.currentTimeMillis should be always greter than starttime .

2010-06-03 21:06:41,129 this is the logged time.

Thanks in advance.
Bennet.

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Weird. Does it happend like this every time you run it?
 
Bennet Xavier
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Not every time... once in a while this problem occurs.

I have tested for 5 times...2 times i recevied the -1.

I am unable to reproduce the same.

Thanks,
Bennet
 
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There may inaccuracies and imprecisions in the milliseconds method. You would do better to find the other methods of the System class which may give you a more precise time.

Some computer clocks only calculate their time to the nearest 0.1s or 0.01s, so that introduces more inaccuracy into the millisecond method. Please confirm that you are applying all your calls to the same machine.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There may inaccuracies and imprecisions in the milliseconds method. You would do better to find the other methods of the System class which may give you a more precise time.

Some computer clocks only calculate their time to the nearest 0.1s or 0.01s, so that introduces more inaccuracy into the millisecond method. Please confirm that you are applying all your calls to the same machine.


Although System.currentTimeMillis() is inaccurate and imprecise, this is the first time I've ever heard of the return value decreasing between subsequent calls. Returning the same value sure, but a lower one? Even if it rounds (up or down) from 0.01s, it should never get a lower value unless the system clock is decreased.
 
Sheriff
Posts: 28321
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Even if it rounds (up or down) from 0.01s, it should never get a lower value unless the system clock is decreased.


It's possible the system clock could be decreased. Many computers these days have a process which checks the computer time against an outside server periodically, adjusting it as required. This adjustment could certainly change the system clock backwards.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is rather awkward. I think I would like to see the whole code. You just wrote an excerpt from it. The problem could be in the code were are not seeing. Could you post an executable example of your code?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:
It's possible the system clock could be decreased. Many computers these days have a process which checks the computer time against an outside server periodically, adjusting it as required. This adjustment could certainly change the system clock backwards.




While I agree that this can happen, it needs to happen between the two calls to fetch the time. And I am very skeptical that it happens often enough for this to happen 2 times out 5 tries. Also, for this to happen, the difference of time must be small -- since you won't see negative numbers if the total is not near zero. Add to that that the drift needs to be greater than the time readings itself... ie. too many things to conclude that there is something else going on.

Henry
 
Campbell Ritchie
Marshal
Posts: 79928
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No longer a "beginning" question. Moving thread.
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic