Tuesday, 19 November 2013

Calculation of Execution /Elapsed Time in Java

Two Java function can be use to calculate the elapsed time or execuiton time in java.

  1. System.currentTimeMillis()
  2. System.nanoTime()

Example:

System.currentTimeMillis()

 long StartTime = System.currentTimeMillis();
 //do some tasks
 long EndTime = System.currentTimeMillis();
 long difference = EndTime - StartTime;
 
 System.out.println("Elapsed milliseconds: " + difference);

System.nanoTime()

 long StartTime = System.nanoTime();
 //do some tasks
 long EndTime = System.nanoTime();
 long difference = EndTime - StartTime;
 
 System.out.println("Elapsed nanoseconds: " + difference);

No comments:

Post a Comment