Skip to content

Commit

Permalink
Simplify the new stopwatch code.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Jul 15, 2018
1 parent f85c9b0 commit 0cf5996
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
44 changes: 33 additions & 11 deletions StopWatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,32 @@
#if defined(_WIN32) || defined(_WIN64)

CStopWatch::CStopWatch() :
m_frequency(),
m_frequencyS(),
m_frequencyMS(),
m_start()
{
::QueryPerformanceFrequency(&m_frequency);
::QueryPerformanceFrequency(&m_frequencyS);

m_frequencyMS.QuadPart = m_frequencyS.QuadPart / 1000ULL;
}

CStopWatch::~CStopWatch()
{
}

unsigned long CStopWatch::start()
unsigned long long CStopWatch::time() const
{
LARGE_INTEGER now;
::QueryPerformanceCounter(&now);

return (unsigned long long)(now.QuadPart / m_frequencyMS.QuadPart);
}

unsigned long long CStopWatch::start()
{
::QueryPerformanceCounter(&m_start);

return (unsigned long)(m_start.QuadPart / m_frequency.QuadPart);
return (unsigned long long)(m_start.QuadPart / m_frequencyS.QuadPart);
}

unsigned int CStopWatch::elapsed()
Expand All @@ -46,7 +57,7 @@ unsigned int CStopWatch::elapsed()
LARGE_INTEGER temp;
temp.QuadPart = (now.QuadPart - m_start.QuadPart) * 1000;

return (unsigned int)(temp.QuadPart / m_frequency.QuadPart);
return (unsigned int)(temp.QuadPart / m_frequencyS.QuadPart);
}

#else
Expand All @@ -55,29 +66,40 @@ unsigned int CStopWatch::elapsed()
#include <ctime>

CStopWatch::CStopWatch() :
m_start()
m_startMS(0ULL)
{
}

CStopWatch::~CStopWatch()
{
}

unsigned long CStopWatch::start()
unsigned long long CStopWatch::time() const
{
::clock_gettime(CLOCK_MONOTONIC, &m_start);
struct timeval now;
::gettimeofday(&now, NULL);

return now.tv_sec * 1000ULL + now.tv_usec / 1000ULL;
}

unsigned long long CStopWatch::start()
{
struct timespec now;
::clock_gettime(CLOCK_MONOTONIC, &now);

m_startMS = now.tv_sec * 1000ULL + now.tv_nsec / 1000000ULL;

return m_start.tv_sec * 1000UL + m_start.tv_nsec / 1000000UL;
return m_startMS;
}

unsigned int CStopWatch::elapsed()
{
struct timespec now;
::clock_gettime(CLOCK_MONOTONIC, &now);

int offset = ((now.tv_sec - m_start.tv_sec) * 1000000000UL + now.tv_nsec - m_start.tv_nsec ) / 1000000UL;
unsigned long long nowMS = now.tv_sec * 1000ULL + now.tv_nsec / 1000000ULL;

return (unsigned int)offset;
return nowMS - m_startMS;
}

#endif
11 changes: 7 additions & 4 deletions StopWatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ class CStopWatch
CStopWatch();
~CStopWatch();

unsigned long start();
unsigned int elapsed();
unsigned long long time() const;

unsigned long long start();
unsigned int elapsed();

private:
#if defined(_WIN32) || defined(_WIN64)
LARGE_INTEGER m_frequency;
LARGE_INTEGER m_frequencyS;
LARGE_INTEGER m_frequencyMS;
LARGE_INTEGER m_start;
#else
struct timespec m_start;
unsigned long long m_startMS;
#endif
};

Expand Down

0 comments on commit 0cf5996

Please sign in to comment.