Calculate process elapsed time with System.nanoTime() in Java

Description

The following code shows how to calculate process elapsed time with System.nanoTime().

Example


/* ww w  . j  a  va 2 s. c  o  m*/
public class Main {
  public static void main(String[] args) {
    long start = System.nanoTime();
    System.out.println("Start: " + start);

    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        System.out.println("asdf");
      }
    }

    long end = System.nanoTime();
    System.out.println("End  : " + end);

    long elapsedTime = end - start;

    System.out.println(elapsedTime + " nano seconds");
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone