/**
 * 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

Ph.D. 2007, working on developing cool imaging technologies for digital cameras, camera phones, and video surveillance cameras.

Buyue Zhang