HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaCritical

How do I time a method's execution in Java?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howjavatimemethodexecution

Problem


  • 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.

Solution

There is always the old-fashioned way:

long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

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

Code Snippets

long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

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

Context

Stack Overflow Q#180158, score: 1480

Revisions (0)

No revisions yet.