0

If I understand correctly, System.currentTimeMillis() is faster, and System.nanoTime() is more accurate.

If I am pinging a server, which in worst case I expect to take...lets go extreme...one minute, is System.currentTimeMillis() still going to be accurate within 1-2 milliseconds? Or should I go with System.nanoTime()?

I am pinging the server like so:

    long startTime = System.nanoTime();
    getResponseFromServer();
    long endTime = System.nanoTime();
    return endTime - startTime;

As you can see, I am currently using System.nanoTime(), but am unsure if I should be using System.currentTimeMillis().

System.currentTimeMillis vs System.nanoTime has a lot of great information on the subject, but not specifics on accuracy of System.currentTimeMillis().

Community
  • 1
  • 1
Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • 1
    You're executing a blocking operation that might tak 1 minute, and are concerned about the performance of nanoTime(), which runs in microseconds (i.e. it runs millions of time faster than the ping). Why? – JB Nizet Apr 03 '15 at 14:08
  • My objective is just to get the ping accurately. I don't care about the blocking. (Maybe because I don't know any better...) – Evorlor Apr 03 '15 at 14:12
  • Then since you know that nanoTime is more accurate, why do you care about its performance? Just use it: it will be fast enough. – JB Nizet Apr 03 '15 at 14:18
  • @JBNizet I do still care about speed. I wish I could better explain myself, but I don't quite understand what I am trying to do either :-) But nanoTime it is. Thanks! – Evorlor Apr 03 '15 at 14:20
  • What do you mean by "get the ping accurately"? As pointed out, the ping operation itself can last from a few seconds to more than a minute. Does it really matter when exactly you trigger an operation which will end in 1, 5, 60 seconds? – GhostCat Apr 03 '15 at 14:21
  • @EddyG Maybe I am wrong on my terminology. By ping, I want to know the time in milliseconds it takes to get the status code of my server. – Evorlor Apr 03 '15 at 14:22
  • OK, so what you actually want to do is to measure the time that it takes from sending something and receiving an answer? Then milli second precision should still be good enough; as in any real world scenario, you are talking about seconds. If you want to use nanos; fine - but that doesn't change the fact that this seems to be "overkill". And worse: because of the fact that there will be many many nanos for your "pings", it gets **much** harder to interpret the corresponding data. You are complicating your statistics for no good reason! – GhostCat Apr 03 '15 at 14:25

1 Answers1

2

Depending on your OS, currentTimeMillis() may have a resolution of up to 15 milliseconds (Windows). On the other hand, the resolution of nanoTime() is about a microsecond on Linux/Mac and its latency is in the tens of nanoseconds. So when you say that nanoTime() is "slow", make sure you understand the actual scale of that qualification.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime is my source with 139 upvotes. Maybe I misread. Thanks! Also, what does "resolution of up to 30 ms" mean? – Evorlor Apr 03 '15 at 14:12
  • 1
    A resolution of 30 ms means that two consecutive calls to that method will never report a difference of less than 30 ms. – Marko Topolnik Apr 03 '15 at 14:17