/**
 * Compute time duration.
 * This class is generally used for analyzing the performance of the part of the program.
 */
public class ExecutionTimer {
	private long start;
	private long end;
 
	public ExecutionTimer() {
		reset();
		start();
	}
 
	/**
	 * Reset timer.
	 */
	public final void reset() {
		start = 0;
		end = 0;
	}
 
	/**
	 * Set start.
	 */
	public final void start() {
		//----- do not use currentTimeMillis method (it is totally inaccurate)
//		start = System.currentTimeMillis();
		start = System.nanoTime();
	}
 
	/**
	 * Set end.
	 * @return
	 */
	public ExecutionTimer end() {
//		end = System.currentTimeMillis();
		end = System.nanoTime();
		return this;
	}
 
	/**
	 * Get duration.
	 * @return
	 */
	public double duration() {
		//----- in millisecond
		return (end - start) / 1000000.0;
	}
 
	public void doAll(String notation) {
		System.out.println(notation + " ---> " + ((System.nanoTime() - start) / 1000000.0));
		start();
	}
}


Back to JavaHowTo

Alumni Liaison

Basic linear algebra uncovers and clarifies very important geometry and algebra.

Dr. Paul Garrett