It can be handy to time code execution so you know how long things take. However, I find the common way this is done sloppy since it's supposed to have the same indentation, which makes it harder to read what's actually being timed.
long start = System.nanoTime();
// The code you want to time
long end = System.nanoTime();
System.out.printf("That took: %d ms.%n", TimeUnit.NANOSECONDS.toMillis(end - start));
I came up with the following, it looks way better, there are a few advantages & disadvantages:
Advantages:
Disadvantages:
AutoClosable
is supposed to be used (pretty sure)TimeCode
which isn't goodtry
block are not accessible outside of itIt can be used like this:
try (TimeCode t = new TimeCode()) {
// The stuff you want to time
}
The code which makes this possible is:
class TimeCode implements AutoCloseable {
private long startTime;
public TimeCode() {
this.startTime = System.nanoTime();
}
@Override
public void close() throws Exception {
long endTime = System.nanoTime();
System.out.printf("That took: %d ms%n",
TimeUnit.NANOSECONDS.toMillis(endTime - this.startTime));
}
}
My question is:
You solution is just fine.
A less expressive way would be to wrap your code to be timed in a lambda.
public void timeCode(Runnable code) {
...
try {
code.run();
} catch ...
}
...
}
timeCode(() -> { ...code to time... });
You would probably like to catch the checked exceptions and pass them to some runtime exception or whatever.