How can I count the time it takes a function to complete in Java?

fmsf picture fmsf · Mar 28, 2009 · Viewed 86.1k times · Source

I need to measure the time it takes for a function to complete in Java. How can I do that?

Note:

I want to measure the function's time consumption, not that of the full program.

Answer

Ande Turner picture Ande Turner · Mar 28, 2009
long start = System.nanoTime();    
methodToBeTimed();
long elapsedTime = System.nanoTime() - start;