Capturing the executing time between 2 statements Java?

Kaddy picture Kaddy · Jul 6, 2010 · Viewed 8.9k times · Source

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

Answer

Ben S picture Ben S · Jul 6, 2010

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: