Java 程序运行计时工具类(on Java 8 上看见的一个工具类觉得挺好记下来)
import static java.util.concurrent.TimeUnit.*;
public class Timer {
private long start = System.nanoTime();
public long duration() {
return NANOSECONDS.toMillis(System.nanoTime() - start);
}
public static long duration(Runnable test) {
Timer timer = new Timer();
test.run();
return timer.duration();
}
public static void main(String[] args) { // 测试下
Timer time = new Timer();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(time.duration());
}
}