System.currentTimeMillis()和System.nanoTime()区别

查看该函数的API,可以知道这个时间的单位是毫秒(相当于当时的时间戳,可以转换成对应的日期的)。而有些程序的执行时间很短,用这个函数计算时间的误差会很大。如就上面这段程序而言,循环次数为100000(10万)时,用时1毫秒,为10000(1万)时,用时0毫秒,更小的肯定也是0。对这类程序而言,这个精确度还远远不够!

API说明:

long java.lang.System.currentTimeMillis()

Returns the current time in milliseconds. Note thatwhile the unit of time of the return value is a millisecond,the granularity of the value depends on the 
underlyingoperating system and may be larger. For example, manyoperating systems measure time in units of tens of milliseconds. 

See the description of the class Date fora discussion of slight discrepancies that may arise between"computer time" and coordinated universal time (UTC).

Returns:the difference, measured in milliseconds, betweenthe current time and midnight, January 1, 1970 UTC.See Also:java.util.Date

翻译:

返回当前时间(以毫秒为单位)。注意,虽然返回值的时间单位是毫秒,但是值的粒度取决于底层操作系统,并且可能更大。例如,许多操作系统以几十毫秒为单位测量时间。

请参阅日期类Date的说明,以讨论“计算机时间”与协调世界时(UTC)之间可能出现的细微差异。

返回:当前时间与协调世界时197011日午夜之间的差值,以毫秒为单位。

这两个方法都是native方法,是基于底层实现的(如不了解,可参考:Java的native方法)。我就没有去深入了解,直接看API:这种方法提供了纳秒精度,只有计算在Java虚拟机的相同实例中获得的两个值之间的差异时,此方法返回的值才有意义。

long java.lang.System.nanoTime()


Returns the current value of the running Java Virtual Machine'shigh-resolution time source, in nanoseconds. 

This method can only be used to measure elapsed time and isnot related to any other notion of system or wall-clock time.
The value returned represents nanoseconds since some fixed butarbitrary origin time (perhaps in the future, so valuesmay be negative). 
The same origin is used by all invocations ofthis method in an instance of a Java virtual machine; othervirtual machine instances are likely to use a different origin. 

This method provides nanosecond precision, but not necessarilynanosecond resolution (that is, 
how frequently the value changes)- no guarantees are made except that the resolution is at least asgood as that of currentTimeMillis(). 

Differences in successive calls that span greater thanapproximately 292 years (263 nanoseconds) will notcorrectly compute elapsed time due to numerical overflow. 

The values returned by this method become meaningful only whenthe difference between two such values, obtained within the sameinstance of a Java virtual machine, is computed. 

For example, to measure how long some code takes to execute: 
 long startTime = System.nanoTime();
 // ... the code being measured ...
 long estimatedTime = System.nanoTime() - startTime;

To compare two nanoTime values 
 long t0 = System.nanoTime();
 ...
 long t1 = System.nanoTime();
one should use t1 - t0 < 0, not t1 < t0,because of the possibility of numerical overflow.

Returns:the current value of the running Java Virtual Machine'shigh-resolution time source, in nanoseconds Since:1.5

翻译:

返回正在运行的Java虚拟机的分辨率时间源的当前值,单位为纳秒。

此方法只能用于测量运行时间,与系统或壁钟时间的任何其他概念无关。
返回的值表示自某个固定的但很小的起始时间(可能在将来,所以值可能是负数)以来的纳秒。
在Java虚拟机实例中,此方法的所有调用都使用相同的源;其他虚拟机实例可能使用不同的起源。

这种方法提供了纳秒精度,但不一定有十亿分之一秒的分辨率(即,
值变化的频率)——除了分辨率至少与currentTimeMillis()一样好之外,没有任何保证。

由于数值溢出,连续调用中超过292(263纳秒)的差异将无法正确计算运行时间。

只有计算在Java虚拟机的相同实例中获得的两个值之间的差异时,此方法返回的值才有意义。

例如,要测量一些代码执行需要多长时间:
long startTime = System.nanoTime();
/ /……正在测量的代码…
long estimatedTime = System.nanoTime() - startTime ;

比较两个纳时值
long t0 = System.nanoTime();long t1 = System.nanoTime();
应该使用t1 - t0 < 0,而不是t1 < t0,因为可能会出现数值溢出。

返回:正在运行的Java虚拟机的分辨率时间源的当前值,以纳秒为单位,从:1.5开始。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值