diff src/win32/PerfTimer.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/PerfTimer.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,403 @@
     1.4 +// CPerfTimer - a simple Win32 performance counter wrapper
     1.5 +// by Dean Wyant dwyant@mindspring.com
     1.6 +
     1.7 +/*
     1.8 +
     1.9 +This class is simple to use. Just declare a variable(s) as type CPerfTimer,
    1.10 +call Start() to start timimg and call Stop() to stop timimg. You can pause a
    1.11 +timer by calling Stop() and then you can call Start() to resume. Retrieve the
    1.12 +elapsed time by calling an Elapsed..() function. Assignment, addition, 
    1.13 +subtraction and comparison are supported. There are a few information calls
    1.14 +available also. All calls except Start and Stop can be performed on a timer
    1.15 +without stopping it.
    1.16 +
    1.17 +*/
    1.18 +
    1.19 +#ifndef __PERFTIMER_H__
    1.20 +#define __PERFTIMER_H__
    1.21 +
    1.22 +class CPerfTimer
    1.23 +{
    1.24 +public:
    1.25 +	CPerfTimer(BOOL bStart = FALSE) {Init(bStart);}
    1.26 +
    1.27 +	CPerfTimer(const CPerfTimer& Src); 
    1.28 +
    1.29 +	virtual ~CPerfTimer() {;}
    1.30 +
    1.31 +	void Start(BOOL bReset = FALSE);   // Start from current value or optionally from 0
    1.32 +	void Stop();                       // Stop timing. Use Start afterwards to continue.
    1.33 +
    1.34 +	BOOL IsRunning();                  // Returns FALSE if stopped.
    1.35 +
    1.36 +	BOOL IsSupported();                // Returns FALSE if performance counter not supported.
    1.37 +	// Call after constructing at least one CPerfTimer
    1.38 +
    1.39 +	const double Resolution();         // Returns timer resolution in seconds
    1.40 +	const double Resolutionms();       // Returns timer resolution in milliseconds
    1.41 +	const double Resolutionus();       // Returns timer resolution in microseconds
    1.42 +
    1.43 +	const double Elapsed();            // Returns elapsed time in seconds
    1.44 +	const double Elapsedms();          // Returns elapsed time in milliseconds 
    1.45 +	const double Elapsedus();          // Returns elapsed time in microseconds
    1.46 +
    1.47 +	const CPerfTimer& operator=(const CPerfTimer& Src); // Assignment operator 
    1.48 +
    1.49 +	// Math operators
    1.50 +	CPerfTimer operator+(const CPerfTimer& Src) const;
    1.51 +	CPerfTimer operator-(const CPerfTimer& Src) const;
    1.52 +	const CPerfTimer& operator+=(const CPerfTimer& Src);
    1.53 +	const CPerfTimer& operator-=(const CPerfTimer& Src);
    1.54 +	// For time in seconds
    1.55 +	CPerfTimer operator+(const double Secs) const;
    1.56 +	CPerfTimer operator-(const double Secs) const;
    1.57 +	const CPerfTimer& operator+=(const double Secs);
    1.58 +	const CPerfTimer& operator-=(const double Secs);
    1.59 +
    1.60 +	// Boolean comparison operators
    1.61 +	BOOL operator<(const CPerfTimer& Src);
    1.62 +	BOOL operator>(const CPerfTimer& Src);
    1.63 +	BOOL operator<=(const CPerfTimer& Src);
    1.64 +	BOOL operator>=(const CPerfTimer& Src);
    1.65 +	// For time in seconds
    1.66 +	BOOL operator<(const double Secs);
    1.67 +	BOOL operator>(const double Secs);
    1.68 +	BOOL operator<=(const double Secs);
    1.69 +	BOOL operator>=(const double Secs);
    1.70 +
    1.71 +	virtual void Lock() const {;}     // Override for thread safe operation
    1.72 +	virtual void Unlock() const {;}     // Override for thread safe operation
    1.73 +protected:
    1.74 +	void Init(BOOL bStart);
    1.75 +	void Copy(const CPerfTimer& Src);
    1.76 +
    1.77 +private:
    1.78 +	__int64 m_Start;
    1.79 +	static __int64 m_Freq;   // does not change while system is running
    1.80 +	static __int64 m_Adjust; // Adjustment time it takes to Start and Stop
    1.81 +};
    1.82 +
    1.83 +class CPerfTimerT : public CPerfTimer
    1.84 +{ // You only need to use types of this class if a timer is going to be shared between threads
    1.85 +public:
    1.86 +	CPerfTimerT(BOOL bStart = FALSE)
    1.87 +	{
    1.88 +		m_hMutex = CreateMutex(NULL,FALSE,"");
    1.89 +		Init(bStart);
    1.90 +	}
    1.91 +
    1.92 +	CPerfTimerT(const CPerfTimerT& Src) 
    1.93 +	{ 
    1.94 +		m_hMutex = CreateMutex(NULL,FALSE,"");
    1.95 +		Copy(Src); 
    1.96 +	}
    1.97 +
    1.98 +	CPerfTimerT(const CPerfTimer& Src) 
    1.99 +	{ 
   1.100 +		m_hMutex = CreateMutex(NULL,FALSE,"");
   1.101 +		Copy(Src); 
   1.102 +	}
   1.103 +
   1.104 +	virtual ~CPerfTimerT() 
   1.105 +	{ CloseHandle(m_hMutex); }
   1.106 +
   1.107 +	const CPerfTimerT& operator=(const CPerfTimerT& Src) // Assignment operator 
   1.108 +	{
   1.109 +		Copy(Src);
   1.110 +		return *this; 
   1.111 +	}
   1.112 +
   1.113 +	virtual void Lock() const { WaitForSingleObject(m_hMutex,10000); }   
   1.114 +	virtual void Unlock() const { ReleaseMutex(m_hMutex); }   
   1.115 +private:
   1.116 +	HANDLE m_hMutex;
   1.117 +};
   1.118 +
   1.119 +inline void CPerfTimer::Init(BOOL bStart)
   1.120 +{
   1.121 +	if (!m_Freq) 
   1.122 +	{ // Initialization should only run once
   1.123 +		QueryPerformanceFrequency((LARGE_INTEGER *)&m_Freq); 
   1.124 +		if (!m_Freq)
   1.125 +			m_Freq = 1; // Timer will be useless but will not cause divide by zero
   1.126 +		m_Start = 0; 
   1.127 +		m_Adjust = 0; 
   1.128 +		Start();            // Time a Stop
   1.129 +		Stop(); 
   1.130 +		m_Adjust = m_Start;
   1.131 +	}
   1.132 +	// This is the only part that normally runs
   1.133 +	m_Start = 0; 
   1.134 +	if (bStart)
   1.135 +		Start(); 
   1.136 +}
   1.137 +
   1.138 +inline CPerfTimer::CPerfTimer(const CPerfTimer& Src)  
   1.139 +{
   1.140 +	Copy(Src);
   1.141 +}
   1.142 +
   1.143 +inline void CPerfTimer::Copy(const CPerfTimer& Src)
   1.144 +{
   1.145 +	if (&Src == this) 
   1.146 +		return; // avoid deadlock if someone tries to copy it to itself
   1.147 +	Src.Lock();
   1.148 +	Lock();
   1.149 +	m_Start = Src.m_Start; 
   1.150 +	Unlock();
   1.151 +	Src.Unlock();
   1.152 +}
   1.153 +
   1.154 +inline void CPerfTimer::Start(BOOL bReset) 
   1.155 +{ // Start from current value or optionally from 0
   1.156 +	__int64 i;
   1.157 +	QueryPerformanceCounter((LARGE_INTEGER *)&i);
   1.158 +	Lock();
   1.159 +	if ((!bReset) && (m_Start < 0))
   1.160 +		m_Start += i;   // We are starting with an accumulated time
   1.161 +	else 
   1.162 +		m_Start = i;    // Starting from 0
   1.163 +	Unlock();
   1.164 +} 
   1.165 +
   1.166 +inline void CPerfTimer::Stop() 
   1.167 +{ // Stop timing. Use Start afterwards to continue
   1.168 +	Lock();
   1.169 +	if (m_Start <= 0)
   1.170 +	{
   1.171 +		Unlock();
   1.172 +		return;          // Was not running
   1.173 +	}
   1.174 +	__int64 i;
   1.175 +	QueryPerformanceCounter((LARGE_INTEGER *)&i); 
   1.176 +	m_Start += -i;          // Stopped timer keeps elapsed timer ticks as a negative 
   1.177 +	if (m_Start < m_Adjust) // Do not overflow
   1.178 +		m_Start -= m_Adjust;  // Adjust for time timer code takes to run
   1.179 +	else 
   1.180 +		m_Start = 0;          // Stop must have been called directly after Start
   1.181 +	Unlock();
   1.182 +} 
   1.183 +
   1.184 +inline BOOL CPerfTimer::IsRunning() 
   1.185 +{ // Returns FALSE if stopped.
   1.186 +	Lock();
   1.187 +	BOOL bRet = (m_Start > 0); // When < 0, holds elpased clicks
   1.188 +	Unlock();
   1.189 +	return bRet;   
   1.190 +}
   1.191 +inline const double CPerfTimer::Elapsed()
   1.192 +{ // Returns elapsed time in seconds
   1.193 +	CPerfTimer Result(*this);
   1.194 +	Result.Stop();
   1.195 +	return (double)(-Result.m_Start)/(double)m_Freq; 
   1.196 +}
   1.197 +
   1.198 +inline const double CPerfTimer::Elapsedms() 
   1.199 +{ // Returns elapsed time in milliseconds
   1.200 +	CPerfTimer Result(*this);
   1.201 +	Result.Stop();
   1.202 +	return (-Result.m_Start*1000.0)/(double)m_Freq; 
   1.203 +}
   1.204 +
   1.205 +inline const double CPerfTimer::Elapsedus() 
   1.206 +{ // Returns elapsed time in microseconds
   1.207 +	CPerfTimer Result(*this);
   1.208 +
   1.209 +	return (-Result.m_Start * 1000000.0)/(double)m_Freq; 
   1.210 +}
   1.211 +
   1.212 +
   1.213 +// Assignment operator
   1.214 +inline const CPerfTimer& CPerfTimer::operator=(const CPerfTimer& Src) 
   1.215 +{
   1.216 +	Copy(Src);
   1.217 +	return *this; 
   1.218 +}
   1.219 +
   1.220 +
   1.221 +// Math operators
   1.222 +inline CPerfTimer CPerfTimer::operator+(const CPerfTimer& Src) const
   1.223 +{
   1.224 +	CPerfTimer Result(*this);
   1.225 +	Result += Src; 
   1.226 +	return Result; 
   1.227 +}
   1.228 +
   1.229 +inline CPerfTimer CPerfTimer::operator-(const CPerfTimer& Src) const
   1.230 +{
   1.231 +	CPerfTimer Result(*this);
   1.232 +	Result -= Src; 
   1.233 +	return Result; 
   1.234 +}
   1.235 +
   1.236 +inline const CPerfTimer& CPerfTimer::operator+=(const CPerfTimer& Src)
   1.237 +{
   1.238 +	CPerfTimer SrcStop(Src);  // Temp is necessary in case Src is not stopped
   1.239 +	SrcStop.Stop();
   1.240 +	Lock();
   1.241 +	m_Start += SrcStop.m_Start;
   1.242 +	Unlock();
   1.243 +	return *this; 
   1.244 +}
   1.245 +
   1.246 +inline const CPerfTimer& CPerfTimer::operator-=(const CPerfTimer& Src)
   1.247 +{
   1.248 +	CPerfTimer SrcStop(Src);  // Temp is necessary in case Src is not stopped
   1.249 +	SrcStop.Stop();
   1.250 +	Lock();
   1.251 +	m_Start -= SrcStop.m_Start; 
   1.252 +	Unlock();
   1.253 +	return *this; 
   1.254 +}
   1.255 +
   1.256 +// For time in seconds
   1.257 +inline CPerfTimer CPerfTimer::operator+(const double Secs) const
   1.258 +{
   1.259 +	CPerfTimer Result(*this);
   1.260 +	Result += Secs; 
   1.261 +	return Result; 
   1.262 +}
   1.263 +
   1.264 +inline CPerfTimer CPerfTimer::operator-(const double Secs) const
   1.265 +{
   1.266 +	CPerfTimer Result(*this);
   1.267 +	Result += Secs; 
   1.268 +	return Result; 
   1.269 +}
   1.270 +
   1.271 +inline const CPerfTimer& CPerfTimer::operator+=(const double Secs)
   1.272 +{
   1.273 +	Lock();
   1.274 +	m_Start -= (__int64)(Secs*(double)m_Freq);
   1.275 +	Unlock();
   1.276 +	return *this; 
   1.277 +}
   1.278 +
   1.279 +inline const CPerfTimer& CPerfTimer::operator-=(const double Secs)
   1.280 +{
   1.281 +	Lock();
   1.282 +	m_Start += (__int64)(Secs*(double)m_Freq);
   1.283 +	Unlock();
   1.284 +	return *this; 
   1.285 +}
   1.286 +
   1.287 +
   1.288 +
   1.289 +// Boolean comparison operators
   1.290 +inline BOOL CPerfTimer::operator<(const CPerfTimer& Src)
   1.291 +{ 
   1.292 +	BOOL bRet; 
   1.293 +	CPerfTimer Temp(Src);
   1.294 +	Lock();
   1.295 +	if (m_Start <= 0)
   1.296 +	{
   1.297 +		Temp.Stop();
   1.298 +		bRet = (m_Start > Temp.m_Start); 
   1.299 +		Unlock();
   1.300 +		return bRet;
   1.301 +	}
   1.302 +	else
   1.303 +		if (Temp.m_Start > 0)
   1.304 +		{
   1.305 +			bRet = (m_Start < Temp.m_Start); 
   1.306 +			Unlock();
   1.307 +			return bRet;
   1.308 +		}
   1.309 +		else
   1.310 +		{
   1.311 +			Unlock();
   1.312 +			CPerfTimer ThisStop(*this);
   1.313 +			ThisStop.Stop();
   1.314 +			return (ThisStop.m_Start > Temp.m_Start); 
   1.315 +		}
   1.316 +}
   1.317 +
   1.318 +inline BOOL CPerfTimer::operator>(const CPerfTimer& Src)
   1.319 +{ 
   1.320 +	BOOL bRet; 
   1.321 +	CPerfTimer Temp(Src);
   1.322 +	Lock();
   1.323 +	if (m_Start <= 0)
   1.324 +	{
   1.325 +		Temp.Stop();
   1.326 +		bRet = (m_Start < Temp.m_Start); 
   1.327 +		Unlock();
   1.328 +		return bRet;
   1.329 +	}
   1.330 +	else
   1.331 +		if (Temp.m_Start > 0)
   1.332 +		{
   1.333 +			bRet = (m_Start > Temp.m_Start); 
   1.334 +			Unlock();
   1.335 +			return bRet;
   1.336 +		}
   1.337 +		else
   1.338 +		{
   1.339 +			Unlock();
   1.340 +			CPerfTimer ThisStop(*this);
   1.341 +			ThisStop.Stop();
   1.342 +			return (ThisStop.m_Start < Temp.m_Start); 
   1.343 +		}
   1.344 +}
   1.345 +
   1.346 +inline BOOL CPerfTimer::operator<=(const CPerfTimer& Src)
   1.347 +{ 
   1.348 +	return !(*this > Src);
   1.349 +}
   1.350 +
   1.351 +inline BOOL CPerfTimer::operator>=(const CPerfTimer& Src)
   1.352 +{ 
   1.353 +	return !(*this < Src);
   1.354 +}
   1.355 +
   1.356 +// For time in seconds
   1.357 +inline BOOL CPerfTimer::operator<(const double Secs)
   1.358 +{ 
   1.359 +	BOOL bRet; 
   1.360 +	Lock();
   1.361 +	if (m_Start <= 0)
   1.362 +	{
   1.363 +		bRet = (m_Start > (__int64)(-Secs*(double)m_Freq)); 
   1.364 +		Unlock();
   1.365 +		return bRet;
   1.366 +	}
   1.367 +	else
   1.368 +	{
   1.369 +		Unlock();
   1.370 +		CPerfTimer ThisStop(*this);
   1.371 +		ThisStop.Stop();
   1.372 +		return (ThisStop.m_Start > (__int64)(-Secs*(double)m_Freq)); 
   1.373 +	}
   1.374 +}
   1.375 +
   1.376 +inline BOOL CPerfTimer::operator>(const double Secs)
   1.377 +{ 
   1.378 +	BOOL bRet; 
   1.379 +	Lock();
   1.380 +	if (m_Start <= 0)
   1.381 +	{
   1.382 +		bRet = (m_Start < (__int64)(-Secs*(double)m_Freq)); 
   1.383 +		Unlock();
   1.384 +		return bRet;
   1.385 +	}
   1.386 +	else
   1.387 +	{
   1.388 +		Unlock();
   1.389 +		CPerfTimer ThisStop(*this);
   1.390 +		ThisStop.Stop();
   1.391 +		return (ThisStop.m_Start < (__int64)(-Secs*(double)m_Freq)); 
   1.392 +	}
   1.393 +}
   1.394 +
   1.395 +inline BOOL CPerfTimer::operator<=(const double Secs)
   1.396 +{ 
   1.397 +	return !(*this > Secs);
   1.398 +}
   1.399 +
   1.400 +inline BOOL CPerfTimer::operator>=(const double Secs)
   1.401 +{ 
   1.402 +	return !(*this < Secs);
   1.403 +}
   1.404 +
   1.405 +
   1.406 +#endif //__PERFTIMER_H__
   1.407 \ No newline at end of file