How to calculate the running time of my program?

Mahdi_Nine picture Mahdi_Nine · Mar 5, 2011 · Viewed 381.4k times · Source

I wrote a program and now I want to calculate the total running time of my program from start to end.

How can I do this?

Answer

snakile picture snakile · Mar 5, 2011

Use System.nanoTime to get the current time.

long startTime = System.nanoTime();
.....your program....
long endTime   = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println(totalTime);

The above code prints the running time of the program in nanoseconds.