How to find time taken to run a Java program?

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:

  • System.nanoTime()

Leave a Comment