Created
March 27, 2018 16:18
-
-
Save eleco/a09126d952a6db1add55d7b8ec3fc148 to your computer and use it in GitHub Desktop.
This file contains 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
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