Java Foundation Consolidation Series (11): Two methods of Java test program run time

Source: Internet
Author: User
Tags time in milliseconds

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.