public class TestApp {
public static void main(String[] args) {
// 1、通过毫秒来计算时间
long start1 = System.currentTimeMillis();
fun();// 要测试的方法
long end1 = System.currentTimeMillis();
System.out.println("用时:" + (end1 - start1)+"毫秒");
// 2、通过纳秒来计算时间
long start2 = System.nanoTime();
fun();// 要测试的方法
long end2 = System.nanoTime();
System.out.println("用时:" + (end2 - start2)+"纳秒");
}
public static void fun() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 1、通过毫秒来计算时间
long start1 = System.currentTimeMillis();
fun();// 要测试的方法
long end1 = System.currentTimeMillis();
System.out.println("用时:" + (end1 - start1)+"毫秒");
// 2、通过纳秒来计算时间
long start2 = System.nanoTime();
fun();// 要测试的方法
long end2 = System.nanoTime();
System.out.println("用时:" + (end2 - start2)+"纳秒");
}
public static void fun() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}