System.nanoTime()

  • It will give you current time in nanoseconds(precise).
  • Usually used for measuring performance of the code.
  • To convert it to in seconds, divide it by 10^9 (1 sec = 1 nano second * 10 ^ 9)

Example…

long before = System.nanoTime();
//
// Some work goes to here
//
long after = System.nanoTime();

// Prints out time taken in nano seconds
System.out.println("Time taken: "+ (after-before) );  

Leave A Comment

Your email address will not be published. Required fields are marked *