ITAStopWatch.cpp 3.79 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
2
3
4
5
6
// $Id: ITAStopWatch.cpp 3923 2015-01-07 16:09:21Z pelzer $

#include <ITAStopWatch.h>

#include <ITAException.h>

js908001's avatar
js908001 committed
7
#include <string>
Jonas Stienen's avatar
Jonas Stienen committed
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
157
158
159
160
161
162
163
164
165
#include <cmath>
#include <float.h>
#include <sstream>

bool ITAStopWatch::m_bInstanceCreated = false;
double ITAStopWatch::m_dStartStopLatency = 0;

ITAStopWatch::ITAStopWatch() 
: m_pClock( ITAClock::getDefaultClock() )
{
	// Falls dies die erste Stopwatch ist die erzeugt wird: Latenz messen
	if (!m_bInstanceCreated) {
		ITACriticalSection cs;
		cs.enter();
		start();
		stop();
		reset();
		for (unsigned int i=0; i<100; i++) {
			start();
			stop();
		}
		cs.leave();
		m_dStartStopLatency = minimum();

		/* DEBUG:
		printf("%s: Measured start/stop latency: %0.3f ns\n",
			   __FUNCTION__, _dStartStopLatency * 1000000000);
		*/

		m_bInstanceCreated = true;
	}

	reset();
}

ITAStopWatch::ITAStopWatch(ITAClock* pClock)
: m_pClock(pClock) {

	if (pClock == NULL)
		ITA_EXCEPT1(INVALID_PARAMETER, "Clock must not be NULL");

	// Falls dies die erste Stopwatch ist die erzeugt wird: Latenz messen
	if (!m_bInstanceCreated) {
		ITACriticalSection cs;
		cs.enter();
		start();
		stop();
		reset();
		for (unsigned int i=0; i<100; i++) {
			start();
			stop();
		}
		cs.leave();
		m_dStartStopLatency = minimum();

		/* DEBUG:
		printf("%s: Measured start/stop latency: %0.3f ns\n",
			   __FUNCTION__, _dStartStopLatency * 1000000000);
		*/

		m_bInstanceCreated = true;
	}

	reset();
}

void ITAStopWatch::reset() 
{
	m_csReadWrite.enter();
	m_uiCycles = 0;
	m_dSum = m_dSquareSum = 0;

	m_dMin = DBL_MAX;
	m_dMax = 0;

	m_bStarted = false;
	m_csReadWrite.leave();
}

bool ITAStopWatch::started() const {  return m_bStarted; }

unsigned int ITAStopWatch::cycles() const
{ 
	m_csReadWrite.enter();
	unsigned int uiResult = m_uiCycles;
	m_csReadWrite.leave();
	return uiResult;
}

/*
void ITAStopWatch::setMaxCycles(unsigned int uiMaxCycles)
{
	m_uiMaxCycles = uiMaxCycles;
}
*/

double ITAStopWatch::minimum() const
{ 
	return (m_uiCycles == 0 ? 0 : m_dMin);
}

double ITAStopWatch::maximum() const {
	return m_dMax;
}

double ITAStopWatch::mean() const
{
	if (m_csReadWrite.tryenter() == true) {
		if (m_uiCycles == 0) {
			m_csReadWrite.leave();
			return -1;
		}
		double dResult = m_dSum / m_uiCycles;
		m_csReadWrite.leave();
		return dResult;
	}

	return -1;
}

double ITAStopWatch::variance() const
{
	if (m_csReadWrite.tryenter() == true) {
		if (m_uiCycles == 0) {
			m_csReadWrite.leave();
			return -1;
		}

		// Formel: sigma = E(X^2) - E(X)^2
		double x = (m_dSum / m_uiCycles);
		double dResult = (m_dSquareSum / m_uiCycles) - (x*x);
		m_csReadWrite.leave();
		return dResult;
	}

	return -1;
}

double ITAStopWatch::std_deviation() const 
{ 
	return sqrt(variance()); 
}

double ITAStopWatch::selftest() 
{
	ITAStopWatch sw;
	sw.start();
	sw.stop();
	sw.reset();
	for (int i=0; i<1000; i++) {
		sw.start();
		sw.stop();
	}
	return sw.mean();
}

std::string TimeToString( double dTimeSeconds)
{
166
167
168
	std::stringstream ss;
	if( dTimeSeconds < 1e-6 )
		ss << dTimeSeconds / 1e-9 << "us";
Jonas Stienen's avatar
Jonas Stienen committed
169
	else if (dTimeSeconds < 1e-3)
170
		ss << dTimeSeconds / 1e-6 << "ns";
Jonas Stienen's avatar
Jonas Stienen committed
171
	else if (dTimeSeconds < 1e-0)
172
		ss << dTimeSeconds / 1e-3 << "ms";
Jonas Stienen's avatar
Jonas Stienen committed
173
	else
174
175
176
		ss << dTimeSeconds << "s";

	return ss.str();
Jonas Stienen's avatar
Jonas Stienen committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
};

std::string ITAStopWatch::ToString() const
{
	std::ostringstream ss;
	ss << "avg=" << TimeToString(mean()) << ", ";
	ss << "stddev=" << TimeToString(std_deviation()) << ", ";
	ss << "min=" << TimeToString(minimum()) << ", ";
	ss << "max=" << TimeToString(maximum()) << ", ";
	ss << "cycles=" << cycles();

	return ss.str();
}

double ITAStopWatch::stop()
{
	m_dStop = m_pClock->getTime();

	double t = m_dStop - m_dStart - m_dStartStopLatency;

	m_csReadWrite.enter();

	m_dMin = (std::min)(m_dMin, t);
	m_dMax = (std::max)(m_dMax, t);
	m_dSum += t;
	m_dSquareSum += (t*t);
	m_uiCycles++;
	m_bStarted = false;

	m_csReadWrite.leave();
	return t;
}