2016-12-17 10:51:25 -08:00
|
|
|
// onjava/Timer.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2016-12-17 10:51:25 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
|
|
// Visit http://OnJava8.com for more book information.
|
|
|
|
package onjava;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|