[Java] Measuring Program Execution Time

Tadashi Shigeoka ·  Sun, October 28, 2012

I’ll introduce how to measure program execution time in Java.

Java

Simply wrap the target process before and after with System.currentTimeMillis() and look at the time difference.

public class Timing {
  public Timing() {}
  public void doSomething() {
    // Do some work
    // Here we simply sleep for 3 seconds
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  public static void main(String[] args) {
    long start = System.currentTimeMillis(); // Start measurement
    Timing timing = new Timing();
    timing.doSomething();    // Call method to do some time-consuming work
    long stop = System.currentTimeMillis(); // End measurement
    System.out.println("実行にかかった時間は " + (stop - start) + " ミリ秒です。");
  }
}

That’s all from the Gemba where I wanted to measure speed in Java.

Reference Information

That’s all from the Gemba.