Javaで処理時間を計測したいときに、便利なロジック。
以下に紹介するクラスをインスタンス化して、計測したい処理の前後でstop()メソッドとstart()メソッドを呼び出すだけ。
サンプルソースコード
/**
* 時間の計測をする。
*
*/
public class Timer {
private long start = 0;
private long stop = 0;
public void start() {
this.start = System.currentTimeMillis();
}
public long stop() {
this.stop = System.currentTimeMillis();
return this.stop - this.start;
}
}
stop()メソッドの戻り値が、ミリ秒単位の処理時間です。


コメント