Last active
October 8, 2018 08:33
-
-
Save mrbald/ad5cc641fd5eccb2022c07ec823d40d6 to your computer and use it in GitHub Desktop.
Java 8 TimeIt combo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.lang.System.nanoTime; | |
/* | |
* Usage: | |
* final long then = TimeIt.start(); | |
* ... | |
* final long elapsed = TimeIt.elapsedSince(then); | |
* recorder.record(elapsed); | |
*/ | |
public class TimeIt { | |
public static long start() { return nanoTime(); } | |
public static long elapsedSince(long then) { | |
final long now = nanoTime(); | |
return now - then; | |
} | |
public static Runnable timed(Runnable runnable, LongConsumer recorder) { | |
final long then = start(); | |
runnable.run(); | |
recorder.record(elapsedSince(then)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment