Two methods in the Java.lang.System class: Currenttimemillis () and Nanotime () methods
First, Nanotime ()
The Nanotime () method is timed in nanoseconds, returning the current value of the most accurate available system timer.
This method can only be used to measure the time that has been spent, regardless of any other time concept of the system or clock time. The return value represents the number of nanoseconds from a fixed but arbitrary time (perhaps from a later date, so the value may be negative). This method provides the accuracy of the nanosecond, but not the accuracy of the nanosecond as necessary. It does not guarantee the change frequency of the value. The difference between successive calls in which the value range is greater than approximately 292 years (263 nanoseconds) is that the time that has been exceeded cannot be accurately calculated due to a digital overflow.
public static void Main (string[] args) {long begin, End;begin = System.nanotime (); StringBuilder S1 = new StringBuilder ("Hello"); S1.append ("World"); end = System.nanotime (); System.out.println (End-begin);//(2) begin = System.nanotime (); StringBuffer s2 = new StringBuffer ("Hello"); S2.append ("World"); end = System.nanotime (); System.out.println (End-begin);}
Second, Currenttimemillis ()
Returns the current time in milliseconds. Note that when the time unit of the return value is milliseconds, the granularity of the value depends on the underlying operating system, and the granularity may be greater. For example, many operating systems measure time in dozens of milliseconds.
Return:
the time difference (measured in milliseconds) between the current and coordinated GMT January 1, 1970 midnight.
public static void Main (string[] args) {Long begin, end;//(1) begin = System.currenttimemillis (); StringBuilder S1 = new StringBuilder ("Hello"); S1.append ("World"); end = System.currenttimemillis (); System.out.println (End-begin);//(2) begin = System.currenttimemillis (); StringBuffer s2 = new StringBuffer ("Hello"); S2.append ("World"); end = System.currenttimemillis (); System.out.println (End-begin);}
Java Foundation Consolidation Series (11): Two methods of Java test program run time