How to find time taken to run a Java program?

MK1 picture MK1 · Jul 11, 2011 · Viewed 89k times · Source

I have a Java application that I've been working on and I just realized that the program has to return a value in less than a minute, but don't know how to find or display the time taken to run the program. How to find time taken to run a program?

Answer

Jigar Joshi picture Jigar Joshi · Jul 11, 2011

You can compare times using System.nanoTime() . It will return the time in nanoseconds.

Returns the current value of the most precise available system timer, in nanoseconds.

You could use it like this:

long startTime = System.nanoTime();

// code

long endTime = System.nanoTime();
System.out.println("Took "+(endTime - startTime) + " ns"); 

Usefull links: