Search code examples
javatimestopwatch

Capturing the executing time between 2 statements Java?


I want to capture the time take to go from statement A to Statement B in a Java class. In between these statements there are many web service calls made. I wanted to know if there is 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: