barrier/lib/base/CStopwatch.cpp

209 lines
3.4 KiB
C++
Raw Normal View History

2001-10-06 18:13:28 +04:00
#include "CStopwatch.h"
//
// CStopwatch
//
CStopwatch::CStopwatch(bool triggered) :
m_mark(0.0),
m_triggered(triggered),
m_stopped(triggered)
2001-10-06 18:13:28 +04:00
{
if (!triggered) {
2001-10-06 18:13:28 +04:00
m_mark = getClock();
}
2001-10-06 18:13:28 +04:00
}
CStopwatch::~CStopwatch()
{
// do nothing
}
double
CStopwatch::reset()
2001-10-06 18:13:28 +04:00
{
if (m_stopped) {
const double dt = m_mark;
m_mark = 0.0;
return dt;
}
else {
const double t = getClock();
const double dt = t - m_mark;
m_mark = t;
return dt;
}
}
void
CStopwatch::stop()
2001-10-06 18:13:28 +04:00
{
if (m_stopped) {
2001-10-06 18:13:28 +04:00
return;
}
2001-10-06 18:13:28 +04:00
// save the elapsed time
m_mark = getClock() - m_mark;
m_stopped = true;
}
void
CStopwatch::start()
2001-10-06 18:13:28 +04:00
{
m_triggered = false;
if (!m_stopped) {
2001-10-06 18:13:28 +04:00
return;
}
2001-10-06 18:13:28 +04:00
// set the mark such that it reports the time elapsed at stop()
m_mark = getClock() - m_mark;
m_stopped = false;
}
void
CStopwatch::setTrigger()
2001-10-06 18:13:28 +04:00
{
stop();
m_triggered = true;
}
double
CStopwatch::getTime()
2001-10-06 18:13:28 +04:00
{
if (m_triggered) {
const double dt = m_mark;
start();
return dt;
}
else if (m_stopped) {
2001-10-06 18:13:28 +04:00
return m_mark;
}
else {
return getClock() - m_mark;
}
2001-10-06 18:13:28 +04:00
}
CStopwatch::operator double()
{
return getTime();
}
bool
CStopwatch::isStopped() const
2001-10-06 18:13:28 +04:00
{
return m_stopped;
}
double
CStopwatch::getTime() const
2001-10-06 18:13:28 +04:00
{
if (m_stopped) {
2001-10-06 18:13:28 +04:00
return m_mark;
}
else {
return getClock() - m_mark;
}
2001-10-06 18:13:28 +04:00
}
CStopwatch::operator double() const
{
return getTime();
}
#if WINDOWS_LIKE
2001-10-06 18:13:28 +04:00
// avoid getting a lot a crap from mmsystem.h that we don't need
#define MMNODRV // Installable driver support
#define MMNOSOUND // Sound support
#define MMNOWAVE // Waveform support
#define MMNOMIDI // MIDI support
#define MMNOAUX // Auxiliary audio support
#define MMNOMIXER // Mixer support
#define MMNOJOY // Joystick support
#define MMNOMCI // MCI support
#define MMNOMMIO // Multimedia file I/O support
#define MMNOMMSYSTEM // General MMSYSTEM functions
#define WIN32_LEAN_AND_MEAN
2001-10-06 18:13:28 +04:00
#include <windows.h>
#include <mmsystem.h>
typedef WINMMAPI DWORD (WINAPI *PTimeGetTime)(void);
static double s_freq = 0.0;
static HINSTANCE s_mmInstance = NULL;
static PTimeGetTime s_tgt = NULL;
//
// initialize local variables
//
class CStopwatchInit {
2002-04-29 18:40:01 +04:00
public:
2001-10-06 18:13:28 +04:00
CStopwatchInit();
~CStopwatchInit();
};
static CStopwatchInit s_init;
CStopwatchInit::CStopwatchInit()
{
LARGE_INTEGER freq;
if (QueryPerformanceFrequency(&freq) && freq.QuadPart != 0) {
s_freq = 1.0 / static_cast<double>(freq.QuadPart);
}
else {
// load winmm.dll and get timeGetTime
s_mmInstance = LoadLibrary("winmm");
if (s_mmInstance) {
2001-10-06 18:13:28 +04:00
s_tgt = (PTimeGetTime)GetProcAddress(s_mmInstance, "timeGetTime");
}
2001-10-06 18:13:28 +04:00
}
}
CStopwatchInit::~CStopwatchInit()
{
if (s_mmInstance) {
2001-10-06 18:13:28 +04:00
FreeLibrary(reinterpret_cast<HMODULE>(s_mmInstance));
}
2001-10-06 18:13:28 +04:00
}
double
CStopwatch::getClock() const
2001-10-06 18:13:28 +04:00
{
// get time. we try three ways, in order of descending precision
if (s_freq != 0.0) {
LARGE_INTEGER c;
QueryPerformanceCounter(&c);
return s_freq * static_cast<double>(c.QuadPart);
}
else if (s_tgt) {
return 0.001 * static_cast<double>(s_tgt());
}
else {
return 0.001 * static_cast<double>(GetTickCount());
}
}
#elif UNIX_LIKE
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
double
CStopwatch::getClock() const
{
struct timeval t;
gettimeofday(&t, NULL);
return (double)t.tv_sec + 1.0e-6 * (double)t.tv_usec;
}
#endif // UNIX_LIKE