Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the method System.currentTimeMillis() really return the current time?

Tags:

java

time

sleep

Based on ideas presented in link I implemented several different "sleep methods". One of this methods was the "binary sleep", which looks like this:

while (System.currentTimeMillis() < nextTimeStamp)
{
sleepTime -= (sleepTime / 2);
sleep(sleepTime);
}

Because the check if the next time step is already reached takes place at the beginning I, would expect that the method is running too long. But the cummulative distribution of the simulation error (expected time - real time) looks like this: alt text http://img267.imageshack.us/img267/4224/errorvscummulativearran.jpg

Does somebody has an idea why I'm getting this results? Maybe the method System.currentTimeMillis() does not really return the current time?

BR,

Markus

@irreputable

When I made the evaluation I also created a bell curve by using a german statistic program. Because it was not possible to change caption, here is the english translation of all relevant items:

Häufigkeit = frequency

Fehler = error

Mittelwert = average

Std-Abw = standard deviation

alt text http://img694.imageshack.us/img694/2254/bellcurve.jpg

like image 432
Markus Avatar asked Dec 14 '09 17:12

Markus


People also ask

How accurate is system currentTimeMillis?

currentTimeMillis() actually give the time accurate to the nearest millisecond on Linux, Mac OS and Windows (and since which versions - I know, for example, that Windows only used to be accurate to the nearest 15/16 milliseconds).

What does system currentTimeMillis return timezone?

json"(February 26, 2019 12:00:00 AM) and that need to be accessed from android app once device's System. currentTimeMillis() returns exact time.

How Fast Is system currentTimeMillis?

System. currentTimeMillis() takes about 29 nanoseconds per call while System. nanoTime() takes about 25 nanoseconds.


1 Answers

No it does not. Its young brother System#nanoTime() has a much better precision than System#currentTimeMillis().

Apart from the answers in their Javadocs (click at the links here above), this subject was discussed several times here as well. Do a search on "currenttimemillis vs nanotime" and you'll get under each this topic: System.currentTimeMillis vs System.nanoTime.

like image 50
BalusC Avatar answered Oct 10 '22 08:10

BalusC