View Javadoc

1   package cz.cuni.amis.utils;
2   
3   public class StopWatch {
4   	
5   	private long start;
6   	
7   	private double time = -1;
8   
9   	public StopWatch() {
10  		start();
11  	}
12  	
13  	/**
14  	 * Watches are start()ed during construction, this will just refresh the start time. 
15  	 */
16  	public void start() {
17  		start = System.nanoTime();
18  	}
19  	
20  	/**
21  	 * In millis
22  	 * @return
23  	 */
24  	public double stop() {
25  		time = (((double)System.nanoTime()) - start)/1000000;
26  		start = System.nanoTime();
27  		return time;
28  	}
29  	
30  	/**
31  	 * In millis
32  	 * @return
33  	 */
34  	public double check() {
35  		time = (((double)System.nanoTime()) - start)/1000000;		
36  		return time;
37  	}
38  	
39  	/**
40  	 * In millis... returns last stop()/check() time. (Use stopStr() and then obtain time with time().)
41  	 * @return
42  	 */
43  	public double time() {
44  		if (time == -1) {
45  			return (((double)System.nanoTime()) - start)/1000000;
46  		}
47  		return time;
48  	}
49  	
50  	public String stopStr() {
51  		return String.format("%.3f", stop()) + " ms";
52  	}
53  	
54  	public String checkStr() {
55  		return String.format("%.3f", check()) + " ms";
56  	}
57  	
58  	public String timeStr() {
59  		return String.format("%.3f", time()) + " ms";
60  	}
61  
62  }