1、使用同一循环条件与循环体,统计三个循环的时间
2、代码:
package main.java.Result.DataStructure;
/**
* 循环效率统计
* @author 晴澜
* @version 2023.03.20
*/
public class Rate {
public static void main(String[] args) {
long start1 = System.currentTimeMillis();
for(int i=1;i<=1000000000;i++){
double num = Math.random();
}
long end1 = System.currentTimeMillis();
System.out.println("for循环使用时间:"+(end1-start1)+"毫秒");
long start2 = System.currentTimeMillis();
int i=1;
while(i<=1000000000){
double num = Math.random();
i++;
}
long end2 = System.currentTimeMillis();
System.out.println("while循环使用时间:"+(end2-start2)+"毫秒");
long start3 = System.currentTimeMillis();
int i2=1;
do{
double num = Math.random();
i2++;
}while(i2<1000000000);
long end3 = System.currentTimeMillis();
System.out.println("do-while循环使用时间:"+(end3-start3)+"毫秒");
}
}
3、结果:
长风破浪会有时,直挂云帆济沧海。