Java precise measurement of code running time code execution time nanoseconds nanotime

Source: Internet
Author: User

Java Precise MeasurementCodeRunning time:

Long starttime = system. nanotime (); // start time
For (INT I = 0; I <10000; I ++ ){
;
}
Long consumingtime = system. nanotime ()-starttime; // time consumed
System. Out. println (consumingtime );
System. Out. println (consumingtime/1000 + "microsecond ");

JDK explanation:

Public static longNanotime()

Returns the current value of the most precise available system timer, 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 time (perhaps in the future, so values may be negative ). this method provides nanosecond precision, but not necessarily nanosecond accuracy. no guarantees are made about how frequently values change. differences in successive cballs that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

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

 
Long starttime = system. nanotime (); //... the code being measured... long estimatedtime = system. nanotime ()-starttime;

Return Value:

The current value of the system timer, in nanoseconds.

Since:

1.5

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.