HOW DO I TIME A METHOD'S EXECUTION IN JAVA?

naomi burke

Total Post:28

Points:196
Posted by  naomi burke
Java 
 1046  View(s)
Ratings:
Rate this:
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. Mayank Tripathi

    Post:397

    Points:3117
    Re: How do I time a method's execution in Java?

    There is always the old-fashioned way:

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!