Skip to content

Instantly share code, notes, and snippets.

@eleco
Created March 27, 2018 16:18
Show Gist options
  • Save eleco/a09126d952a6db1add55d7b8ec3fc148 to your computer and use it in GitHub Desktop.
Save eleco/a09126d952a6db1add55d7b8ec3fc148 to your computer and use it in GitHub Desktop.
public class PerformanceUtil {
/*
A utility to detect slow running functions
*/
public static final int WARN_THRESHOLD_MILLIS =1000;
public static <T> T timed(Executable<T> function) {
long start = System.currentTimeMillis();
try {
return function.execute();
} finally {
long elapsed = System.currentTimeMillis() - start;
if (elapsed > WARN_THRESHOLD_MILLIS) {
System.out.println("Method execution slow, took:" + elapsed + " ms.");
}
}
}
public interface Executable<T> {
T execute();
}
public static void main(String args[]) {
//example use case
PerformanceUtil.timed(() -> Thread.getAllStackTraces().size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment