Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a command in java to measure the execution time?

Is there a command in java to measure the execution time ?

Something like

System.out.println(execution.time);

in the end of the code.

like image 702
user680406 Avatar asked Dec 01 '22 03:12

user680406


1 Answers

Here is a complete and little modified example on how you could do that:

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

  public ExecutionTimer() {
    reset();
    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();
    for (int i = 0; i < 80; i++){
System.out.print(".");
}
    t.end();
    System.out.println("\n" + t.duration() + " ms");
  }
}
like image 162
RoflcoptrException Avatar answered Dec 05 '22 10:12

RoflcoptrException