Java/Timing: Difference between revisions
From charlesreid1
No edit summary |
No edit summary |
||
| Line 3: | Line 3: | ||
If you just want to see how much time a piece of code takes to execute, you can use Java's built in time functionality: | If you just want to see how much time a piece of code takes to execute, you can use Java's built in time functionality: | ||
< | <pre> | ||
long start = System.nanoTime(); | long start = System.nanoTime(); | ||
doStuff(); | doStuff(); | ||
| Line 9: | Line 9: | ||
long duration = end - start; | long duration = end - start; | ||
System.out.printf("Elapsed time: %03f s\n", duration/1E9); | System.out.printf("Elapsed time: %03f s\n", duration/1E9); | ||
</ | </pre> | ||
Revision as of 11:33, 30 March 2017
If you want an extremely detailed picture of how much time you're spending in the various parts of your code, you can use a profiler: see Java/Profiling
If you just want to see how much time a piece of code takes to execute, you can use Java's built in time functionality:
long start = System.nanoTime();
doStuff();
long end = System.nanoTime();
long duration = end - start;
System.out.printf("Elapsed time: %03f s\n", duration/1E9);