Summary of methods for calculating program code execution time in Java

This paper summarizes the method of calculating the execution time of Java program code. Share with you for your reference, as follows:

Sometimes, in order to troubleshoot performance problems, it is necessary to record the time required to complete an operation. We can use the currenttimemillis() method of the system class to return the current number of milliseconds and save it in a variable. After the method is executed, call the currenttimemillis() method of the system again and calculate the difference between the two calls, Is the number of milliseconds consumed by method execution.

As shown in method 1:

The second method is calculated in nanoseconds (using system's nanotime() method):

Example code 1:

Example code 2:

The output results are similar:

Readers interested in more Java related content can view the special topics of this site: summary of Java date and time operation skills, tutorial on Java data structure and algorithm, summary of Java DOM node operation skills and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>