7

I got an interesting "time-travel" problem today, using the following code:

for (int i = 0; i < 1; i++){
    long start = System.currentTimeMillis();
    // Some code here
    System.out.print(i + "\t" + (System.currentTimeMillis() - start));
    start = System.currentTimeMillis();
    // Some code here
    System.out.println("\t" + (System.currentTimeMillis() - start));
}

And I got the result

0   15  -606

And it seems that it is not repeatable. Anyone has any clues on what happened inside during the running time? Just curious...

New edit: I used a small test to confirmed the answers below. I run the program and change the system time during the run, and finally repeat the "time-travel":

0   -3563323    163

Case closed. Thanks guys!

More words: both currentTimeMillis() and nanoTime() are system-timer based, so they will be not monotonic if the system timer is updated (turned back, specifically). It is better to use some internet-based timer for such cases.

asksw0rder
  • 1,066
  • 1
  • 12
  • 19
  • Not sure `currentTimeMillis` is monotonic. You may have better results wit `System.nanoTime()` which is designed especially to measure intervals. – Rom1 Jan 22 '12 at 10:46
  • @asksw0rder : can you run same program again and update what second output is? – Fahim Parkar Jan 22 '12 at 10:47
  • @asksw0rder What is the variable i? what is it value? Makes no sense to get 0 in the first output. – javing Jan 22 '12 at 10:48
  • Possible Duplicates: [How do I measure elapsed time in Java?][1] [1]: http://stackoverflow.com/questions/1770010/how-do-i-measure-time-elapsed-in-java – bchetty Jan 22 '12 at 10:49
  • @bchetty : not duplicate... plsss.... – Fahim Parkar Jan 22 '12 at 10:50
  • 2
    Are you using some sort of network time synchronization to keep your computer time up-to-date? I guess it could have been caused by the synchronization correcting "drift" in the system clock. – esaj Jan 22 '12 at 10:50
  • @FahimParkar Cannot repeat it any more... So I think it is just because that the system time is changed. – asksw0rder Jan 22 '12 at 13:19
  • @sfrj i is actually for a loop starting from 0... I missed the code for the loop... – asksw0rder Jan 22 '12 at 13:20
  • hmmm... Cool that problem is solved... – Fahim Parkar Jan 22 '12 at 15:22

2 Answers2

12

System.currentTimeMillis() depends on the system time. So it could be modified by third party systems.

For measuring time is System.nanoTime() the better option.

Robin
  • 8,162
  • 7
  • 56
  • 101
  • nanoTime() is not ok for such case either. It is better to use internet-based time. – asksw0rder Jan 22 '12 at 13:42
  • I didn't say that it's the best option, just a better than `System.currentTimeMillis()`! .. it's tricky to measure nano seconds over the internet. – Robin Jan 22 '12 at 14:16
0

I recall something like time adjustments made to the systems time once in a while too match actual time. And since currentTimeMillis relies on the system clock that might have happened. Also are you synchronizing with a time server, that could also be.

stryba
  • 1,979
  • 13
  • 19