0 votes
1 view
in Java by (2.6k points)

How do I get a method's execution time? Is there a Timer utility class for things like timing how long a task takes, etc?

Most of the searches on Google return results for timers that schedule threads and tasks, which is not what I want.

1 Answer

0 votes
by (46.1k points)

To get a method's execution time use:

long startTime = System.nanoTime();

methodToTime();

long endTime = System.nanoTime();

long duration = (endTime - startTime);  //divide by 1000000 to get milliseconds.

Of course, the warnings about using the wall clock apply: influences of JIT-compilation, multiple, etc. Thus, you need to first execute the method a lot of times first, such that the JIT compiler does its work, and then recur this test various times and take the lowest execution time

Related questions

0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...