Last active
January 14, 2020 10:13
-
-
Save aslamanver/582a8708263e197ba80dd0a1ce772136 to your computer and use it in GitHub Desktop.
Profile the elapsed time of code blocks in milliseconds using ProfileTime class.
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
// Sample | |
ProfileTime.exec("myMethod", new Runnable() { | |
@Override | |
public void run() { | |
myMethod(); | |
} | |
}); | |
// Output - myMethod => elapsed: 9 |
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
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