共用方式為


JavaSystem.NanoTime Method

Definition

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

[Android.Runtime.Register("nanoTime", "()J", "")]
public static long NanoTime ();
[<Android.Runtime.Register("nanoTime", "()J", "")>]
static member NanoTime : unit -> int64

Returns

the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds

Attributes

Remarks

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

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

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

Differences in successive calls that span greater than approximately 292 years (2<sup>63</sup> nanoseconds) will not correctly compute elapsed time due to numerical overflow.

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

For example, to measure how long some code takes to execute:

{@code
            long startTime = System.nanoTime();
            // ... the code being measured ...
            long estimatedTime = System.nanoTime() - startTime;}

To compare two nanoTime values

{@code
            long t0 = System.nanoTime();
            ...
            long t1 = System.nanoTime();}

one should use t1 - t0 < 0, not t1 < t0, because of the possibility of numerical overflow.

The value returned by this method does not account for elapsed time during deep sleep. For timekeeping facilities available on Android see android.os.SystemClock.

Added in 1.5.

Java documentation for java.lang.System.nanoTime().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to