• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Problem in time calculation.  RSS feed

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 64479
225
  • Mark post as helpful
  • send pies
  • 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: 21741
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 64479
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No longer a "beginning" question. Moving thread.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!