javatimestopwatch

Capturing the executing time between 2 statements Java?


I want to capture the time it takes to go from statement A to Statement B in a Java class. In my code, in between these statements A and B, there are many web service calls made. Is there some stop-watch-like functionality in Java that I could use to capture the exact time?

-Kaddy


Solution

  • This will give you the number of nanoseconds between the two nanoTime() calls.

    long start = System.nanoTime();
    // Java statements
    long diff = System.nanoTime() - start;
    

    For more sophisticated approaches there are several duplicate questions that address Stopwatch classes: