Java Measure Time Code Example

Snippet 1
  final long startTime = System.currentTimeMillis();
for (int i = 0; i < length; i++) {
  // Do something
}
final long endTime = System.currentTimeMillis();

System.out.println("Total execution time: " + (endTime - startTime)); 
Snippet 2
  long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

long duration = (endTime - startTime);  //divide by 1000000 to get milliseconds. 
Snippet 3
  long start = System.nanoTime();
long finish = System.nanoTime();
long timeElapsed = finish - start;