如何在Java中测量经过时间?
一般来说,经过时间是指事件从起点到终点的这段时间。以下是几种在Java中查找经过时间的方法:
使用currentTimeMillis()方法
currentTimeMillis()方法以毫秒为单位返回当前时间。要查找方法的经过时间,您可以获取所需方法执行前后时间值之间的差值。
示例
public class Example { public void test(){ int num = 0; for(int i=0; i<=50; i++){ num =num+i; System.out.print(num+", "); } } public static void main(String args[]){ //Start time long begin = System.currentTimeMillis(); //Starting the watch new Example().test(); //End time long end = System.currentTimeMillis(); long time = end-begin; System.out.println(); System.out.println("Elapsed Time: "+time +" milli seconds"); } }
输出
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time: 4 milli seconds
使用nanoTime()方法
nanoTime()方法以纳秒为单位返回当前时间。要查找方法的经过时间,您可以获取所需方法执行前后时间值之间的差值。
示例
public class Example { public void test(){ int num = 0; for(int i=0; i<=50; i++){ num =num+i; System.out.print(num+", "); } } public static void main(String args[]){ //Start time long begin = System.nanoTime(); //Starting the watch new Example().test(); //End time long end = System.nanoTime(); long time = end-begin; System.out.println(); System.out.println("Elapsed Time: "+time); } }
输出
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time: 1530200
使用Instant类
Instant类的now()方法返回当前时间,Duration.between()方法返回给定两个时间值之间的差值,以获取经过时间,在所需方法执行前后检索时间值,并使用Duration.between()方法检索持续时间。
示例
import java.time.Duration; import java.time.Instant; public class Example { public void test(){ int num = 0; for(int i=0; i<=50; i++){ num =num+i; System.out.print(num+", "); } } public static void main(String args[]) { //Starting time Instant start = Instant.now(); new Example().test(); //End time Instant end = Instant.now(); long time = Duration.between(start, end).toMillis(); System.out.println(); System.out.println(time+" Milli seconds"); } }
输出
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, 3 Milli seconds
使用StopWatch类
Apache commons库提供了一个名为Stopwatch的类,它提供start()、stop()和getTime()方法来查找方法执行所需的时间。
以下是此包的Maven文件:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency>
示例
import org.apache.commons.lang3.time.StopWatch; public class Example { public void test(){ int num = 0; for(int i=0; i<=50; i++){ num =num+i; System.out.print(num+", "); } } public static void main(String args[]) { //Instantiating the StopWatch class StopWatch obj = new StopWatch(); //Starting the watch obj.start(); new Example().test(); //Stopping the watch obj.stop(); System.out.println(); System.out.println("Elapsed Time: "+obj.getTime() +" milli seconds"); } }
输出
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, Elapsed Time: 1 milli seconds
广告