System.CurrentTimeMillis 是否足够准确以 ping 服务器?
Is System.CurrentTimeMillis accurate enough to ping a server?
如果我没理解错的话,System.currentTimeMillis() is faster, and System.nanoTime()更准确。
如果我正在对服务器执行 ping 操作,在最坏的情况下我希望...让我们走极端...一分钟,System.currentTimeMillis() still going to be accurate within 1-2 milliseconds? Or should I go with System.nanoTime()?
我正在像这样 ping 服务器:
long startTime = System.nanoTime();
getResponseFromServer();
long endTime = System.nanoTime();
return endTime - startTime;
如您所见,我目前正在使用 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().
根据您的 OS,currentTimeMillis()
的分辨率可能高达 15 毫秒 (Windows)。另一方面,nanoTime()
的分辨率在 Linux/Mac 上约为一微秒,其延迟为数十纳秒。因此,当您说 nanoTime()
是 "slow" 时,请确保您了解该资格的实际规模。
如果我没理解错的话,System.currentTimeMillis() is faster, and System.nanoTime()更准确。
如果我正在对服务器执行 ping 操作,在最坏的情况下我希望...让我们走极端...一分钟,System.currentTimeMillis() still going to be accurate within 1-2 milliseconds? Or should I go with System.nanoTime()?
我正在像这样 ping 服务器:
long startTime = System.nanoTime();
getResponseFromServer();
long endTime = System.nanoTime();
return endTime - startTime;
如您所见,我目前正在使用 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().
根据您的 OS,currentTimeMillis()
的分辨率可能高达 15 毫秒 (Windows)。另一方面,nanoTime()
的分辨率在 Linux/Mac 上约为一微秒,其延迟为数十纳秒。因此,当您说 nanoTime()
是 "slow" 时,请确保您了解该资格的实际规模。