Skip to content

Instantly share code, notes, and snippets.

@aslamanver
Last active January 14, 2020 10:13
Show Gist options
  • Save aslamanver/582a8708263e197ba80dd0a1ce772136 to your computer and use it in GitHub Desktop.
Save aslamanver/582a8708263e197ba80dd0a1ce772136 to your computer and use it in GitHub Desktop.
Profile the elapsed time of code blocks in milliseconds using ProfileTime class.
// Sample
ProfileTime.exec("myMethod", new Runnable() {
@Override
public void run() {
myMethod();
}
});
// Output - myMethod => elapsed: 9
import java.util.concurrent.TimeUnit;
public class ProfileTime {
public static void exec(String name, Runnable runnable) {
long start = System.nanoTime();
runnable.run();
long elapsed = System.nanoTime() - start;
print(name, elapsed);
}
private static void print(String name, long elapsed) {
System.out.println("*********************************");
System.out.println("* *");
System.out.println(" " + name + " => " + "elapsed: " + TimeUnit.MILLISECONDS.convert(elapsed, TimeUnit.NANOSECONDS));
System.out.println("* *");
System.out.println("*********************************");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment