This is the Java code:
public void foo() {
final long start = System.nanoTime();
// some operations...
System.out.println("done in " + (System.nanoTime() - start) + " nano sec");
}
Is it possible to do the same, but without start variable? Something like this:
public void foo() {
// some operations...
System.out.println("done in " + calculateTime() + " nano sec");
}
This is not a straightforward answer. I’m inferring from the question that you do not wish to clutter your code with unnecessary profiling code, that you may remove later. In such a case, it is recommended to use AOP. You may then weave aspects around the methods that you want profiled, and these aspects would have the variable declarations that represent the ‘start’ and ‘end’ variables.