20 lines
537 B
Java
20 lines
537 B
Java
|
// onjava/Timer.java
|
||
|
// (c)2016 MindView LLC: see Copyright.txt
|
||
|
// 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();
|
||
|
}
|
||
|
}
|