Share this page 

Time the executionTag(s): Environment


public class ExecutionTimer {
  private long start;
  private long end;

  public ExecutionTimer() {
    reset();
  }

  public void start() {
    start = System.currentTimeMillis();
  }

  public void end() {
    end = System.currentTimeMillis();
  }

  public long duration(){
    return (end-start);
  }

  public void reset() {
    start = 0;  
    end   = 0;
  }

  public static void main(String s[]) {
    // simple example
    ExecutionTimer t = new ExecutionTimer();
    t.start();
    for (int i=0; i < 80; i++){ System.out.print(".");}
    t.end();
    System.out.println("\n" + t.duration() + " ms");
  }
}
See this HowTo to format a duration in ms into a string as "Days , Hours , minutes and seconds".