summaryrefslogtreecommitdiff
path: root/trunk/users/hoeken/gcode-host-old/src/org/reprap/utilities/Timer.java
blob: 4dd978650eddd1ca4e20ccbd07150919dd2798da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 * 
 */
package org.reprap.utilities;

import java.util.*;
import java.text.DecimalFormat;

/**
 * @author Adrian
 *
 * I bet there's a system utility somewhere to do this...
 * 
 */
public class Timer {
	
	/**
	 * Time at the start
	 */
	private long t0 = 0;
	
	/**
	 * Time now
	 */
	private long t = 0;
	
	/**
	 * Time since last call
	 */
	private long delta;
	
	/**
	 * for 3 d.p.
	 */
	private DecimalFormat threeDec;
	
	/**
	 * Static single instance to hold all times
	 */
	static private Timer tim = null;
	
	/**
	 * Constructor just needs to create a single 
	 * instance for initialiseIfNeedBe(String e)
	 *
	 */
	private Timer() {}
	
	/**
	 * What o'clock have you?
	 *
	 */
	static private void newTime()
	{
		long last = tim.t;
		Date d = new Date();
		tim.t = d.getTime() - tim.t0;
		tim.delta = tim.t - last;
	}
	
	/**
	 * Check if we've been initialised and initialise if needed
	 * @param e
	 */
	static private void initialiseIfNeedBe()
	{
		if(tim != null) return;
		tim = new Timer();
		
		newTime();
		tim.t0 = tim.t;
		tim.threeDec = new DecimalFormat("0.000");
		tim.threeDec.setGroupingUsed(false);
	}
	
	/**
	 * Get a double as a 3 d.p. string
	 * @param v
	 * @return
	 */
	static private String d3dp(double v)
	{
		return tim.threeDec.format(v);
	}
	
	/**
	 * Generate a timestamp
	 * @return
	 */
	static public String stamp()
	{
		initialiseIfNeedBe();
		newTime();
		return " [" + d3dp(tim.t*0.001) + "s/" + tim.delta + "ms]";
	}
}