File: Timing.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (156 lines) | stat: -rw-r--r-- 5,824 bytes parent folder | download | duplicates (3)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#pragma once

namespace storm {
	STORM_PKG(core);

	class Str;

	/**
	 * Basic time api for measuring short intervals.
	 */

	/**
	 * Represents a specific point in time.
	 */
	class Moment {
		STORM_VALUE;
	public:
		// Create a representation of *now*
		STORM_CTOR Moment();

		// Inernal use, create with specific value.
		Moment(Long v);

		// The time value, measured in us from some point in time.
		Long v;

		// To string.
		void STORM_FN toS(StrBuf *to) const;
	};

	/**
	 * Represents a difference between two points in time.
	 */
	class Duration {
		STORM_VALUE;
	public:
		// Zero duration.
		STORM_CTOR Duration();

		// Create a duration with a time in us.
		Duration(Long t);

		// The actual value, in us.
		Long v;

		// Get the duration in microseconds.
		inline Long STORM_FN inUs() const { return Long(v); }

		// Get the duration in milliseconds.
		inline Long STORM_FN inMs() const { return Long(v / 1000); }

		// Get the duration in seconds.
		inline Long STORM_FN inS() const { return Long(v / 1000000); }

		// Get the duration in minutes.
		inline Long STORM_FN inMin() const { return inS() / 60; }

		// Get the duration in hours.
		inline Long STORM_FN inH() const { return inMin() / 60; }

		// To string.
		void STORM_FN toS(StrBuf *to) const;
	};

	namespace time {
		// Create durations in various units.
		Duration STORM_FN h(Long v);
		Duration STORM_FN min(Long v);
		Duration STORM_FN s(Long v);
		Duration STORM_FN ms(Long v);
		Duration STORM_FN us(Long v);
	}

	// Sleep for the specified duration. Do not expect more than ms precision.
	void STORM_FN sleep(Duration d);

	// Yield the current thread. This causes the current UThread to stop running, and the system
	// will schedule another thread that is ready to run if possible. The current thread is still
	// marked as ready, and will continue to execute whenever the scheduler schedules it again.
	void STORM_FN yield();

	// Output.
	wostream &operator <<(wostream &to, Moment m);
	wostream &operator <<(wostream &to, Duration d);

	// Arithmetic operations between durations and moments.
	inline Duration STORM_FN operator -(Moment a, Moment b) { return Duration(a.v - b.v); }
	inline Moment STORM_FN operator +(Moment a, Duration b) { return Moment(a.v + b.v); }
	inline Moment STORM_FN operator +(Duration b, Moment a) { return Moment(a.v + b.v); }
	inline Moment STORM_FN operator -(Moment a, Duration b) { return Moment(a.v - b.v); }
	inline Moment &STORM_FN operator +=(Moment &a, Duration b) { a.v += b.v; return a; }
	inline Moment &STORM_FN operator -=(Moment &a, Duration b) { a.v -= b.v; return a; }

	inline Duration STORM_FN operator +(Duration a, Duration b) { return Duration(a.v + b.v); }
	inline Duration STORM_FN operator -(Duration a, Duration b) { return Duration(a.v - b.v); }
	inline Duration STORM_FN operator *(Duration a, Int factor) { return Duration(a.v * factor); }
	inline Duration STORM_FN operator /(Duration a, Int factor) { return Duration(a.v / factor); }
	inline Duration STORM_FN operator *(Duration a, Float factor) { return Duration(Long(a.v * Double(factor))); }
	inline Duration STORM_FN operator /(Duration a, Float factor) { return Duration(Long(a.v / Double(factor))); }
	inline Float STORM_FN operator /(Duration a, Duration b) { return float(double(a.v) / double(b.v)); }
	inline Duration STORM_FN operator %(Duration a, Duration b) { return Duration(a.v % b.v); }
	inline Duration &STORM_FN operator +=(Duration &a, Duration b) { a.v += b.v; return a; }
	inline Duration &STORM_FN operator -=(Duration &a, Duration b) { a.v -= b.v; return a; }
	inline Duration &STORM_FN operator *=(Duration &a, Int b) { a.v *= b; return a; }
	inline Duration &STORM_FN operator /=(Duration &a, Int b) { a.v /= b; return a; }
	inline Duration &STORM_FN operator *=(Duration &a, Float b) { a.v = Long(a.v * Double(b)); return a; }
	inline Duration &STORM_FN operator /=(Duration &a, Float b) { a.v = Long(a.v / Double(b)); return a; }

	// Comparisons. TODO: Consider wrapping of Moments.
	inline Bool STORM_FN operator ==(Moment a, Moment b) { return a.v == b.v; }
	inline Bool STORM_FN operator !=(Moment a, Moment b) { return a.v != b.v; }
	inline Bool STORM_FN operator >(Moment a, Moment b) { return a.v > b.v; }
	inline Bool STORM_FN operator <(Moment a, Moment b) { return a.v < b.v; }
	inline Bool STORM_FN operator >=(Moment a, Moment b) { return a.v >= b.v; }
	inline Bool STORM_FN operator <=(Moment a, Moment b) { return a.v <= b.v; }

	inline Bool STORM_FN operator ==(Duration a, Duration b) { return a.v == b.v; }
	inline Bool STORM_FN operator !=(Duration a, Duration b) { return a.v != b.v; }
	inline Bool STORM_FN operator <(Duration a, Duration b) { return a.v < b.v; }
	inline Bool STORM_FN operator >(Duration a, Duration b) { return a.v > b.v; }
	inline Bool STORM_FN operator <=(Duration a, Duration b) { return a.v <= b.v; }
	inline Bool STORM_FN operator >=(Duration a, Duration b) { return a.v >= b.v; }

	// Tell Storm about these, without disturbing the C++ namespace.
	namespace impl {
		inline Duration STORM_FN min(Duration a, Duration b) { return ::min(a, b); }
		inline Duration STORM_FN max(Duration a, Duration b) { return ::max(a, b); }
	}

	/**
	 * Simple way of measuring accumulated time of calls. Results are printed at the end of the
	 * program execution. No effort has been made to make it work through DLL boundaries.
	 *
	 * Usage:
	 * CHECK_TIME(L"<identifier>");
	 *
	 * All timings with the same identifier are grouped together at the end.
	 */
	class CheckTime : NoCopy {
	public:
		CheckTime(const wchar_t *id) : id(id), started() {}

		~CheckTime() {
			save(id, Moment() - started);
		}

	private:
		const wchar_t *id;
		Moment started;

		static void save(const wchar_t *id, const Duration &d);
	};

#define CHECK_TIME(id) CheckTime _x(id);

}