Commit b78bc93f authored by Douglas Serson's avatar Douglas Serson

Use chrono to simplify timer

parent b149b3a1
......@@ -37,91 +37,35 @@
namespace Nektar
{
Timer::Timer() :
m_start(),
m_end(),
m_resolution()
{
}
Timer::~Timer()
{
}
void Timer::Start()
{
#ifdef _WIN32
QueryPerformanceCounter(&m_start);
#elif defined(__APPLE__)
gettimeofday(&m_start, 0);
#else
clock_gettime(CLOCK_REALTIME, &m_start);
#endif
}
Timer::Timer() :
m_start(),
m_end()
{
}
void Timer::Stop()
{
#ifdef _WIN32
QueryPerformanceCounter(&m_end);
#elif defined(__APPLE__)
gettimeofday(&m_end, 0);
#else
clock_gettime(CLOCK_REALTIME, &m_end);
#endif
}
Timer::~Timer()
{
}
Timer::CounterType Timer::Elapsed()
{
#ifdef _WIN32
CounterType result;
result.QuadPart = m_end.QuadPart - m_start.QuadPart;
return result;
#elif defined(__APPLE__)
CounterType result = m_end;
if( result.tv_usec < m_start.tv_usec)
{
result.tv_sec -= 1;
result.tv_usec += 1000000;
}
result.tv_sec -= m_start.tv_sec;
result.tv_usec -= m_start.tv_usec;
return result;
#else
CounterType result = m_end;
void Timer::Start()
{
m_start = Clock::now();
}
if( result.tv_nsec < m_start.tv_nsec)
{
result.tv_sec -= 1;
result.tv_nsec += 1000000000;
}
result.tv_sec -= m_start.tv_sec;
result.tv_nsec -= m_start.tv_nsec;
void Timer::Stop()
{
m_end = Clock::now();
}
return result;
#endif
}
Timer::Seconds Timer::Elapsed()
{
return std::chrono::duration_cast<Seconds>(m_end - m_start);
}
NekDouble Timer::TimePerTest(unsigned int n)
{
#ifdef _WIN32
CounterType frequency;
QueryPerformanceFrequency(&frequency);
return Elapsed().QuadPart/static_cast<NekDouble>(n) * 1.0/frequency.QuadPart;
#elif defined(__APPLE__)
CounterType elapsed = Elapsed();
NekDouble result = elapsed.tv_sec/static_cast<NekDouble>(n) +
( elapsed.tv_usec/static_cast<NekDouble>(n) * 1.0e-6);
return result;
#else
CounterType elapsed = Elapsed();
NekDouble result = elapsed.tv_sec/static_cast<NekDouble>(n) +
( elapsed.tv_nsec/static_cast<NekDouble>(n) * 1.0e-9);
return result;
#endif
}
NekDouble Timer::TimePerTest(unsigned int n)
{
return Elapsed().count() / static_cast<NekDouble>(n);
}
}
}
......@@ -37,50 +37,41 @@
#ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_TIMER_H
#define NEKTAR_LIB_UTILITIES_BASIC_UTILS_TIMER_H
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#else
#include <sys/time.h>
#include <time.h>
#endif
#include <chrono>
#include <LibUtilities/LibUtilitiesDeclspec.h>
#include <LibUtilities/BasicConst/NektarUnivConsts.hpp>
namespace Nektar
{
class Timer
{
public:
#ifdef _WIN32
typedef LARGE_INTEGER CounterType;
#elif defined(__APPLE__)
typedef timeval CounterType;
#else
typedef timespec CounterType;
#endif
public:
LIB_UTILITIES_EXPORT Timer();
LIB_UTILITIES_EXPORT ~Timer();
class Timer
{
public:
using Clock = std::chrono::steady_clock;
using CounterType = Clock::time_point;
using Seconds = std::chrono::duration<NekDouble>;
public:
LIB_UTILITIES_EXPORT Timer();
LIB_UTILITIES_EXPORT ~Timer();
Timer(const Timer& rhs) = delete;
Timer& operator=(const Timer& rhs) = delete;
LIB_UTILITIES_EXPORT void Start();
LIB_UTILITIES_EXPORT void Stop();
LIB_UTILITIES_EXPORT CounterType Elapsed();
LIB_UTILITIES_EXPORT void Start();
LIB_UTILITIES_EXPORT void Stop();
LIB_UTILITIES_EXPORT Seconds Elapsed();
/// \brief Returns amount of seconds per iteration in
/// a test with n iterations.
LIB_UTILITIES_EXPORT NekDouble TimePerTest(unsigned int n);
/// \brief Returns amount of seconds per iteration in
/// a test with n iterations.
LIB_UTILITIES_EXPORT NekDouble TimePerTest(unsigned int n);
private:
Timer(const Timer& rhs);
Timer& operator=(const Timer& rhs);
private:
CounterType m_start;
CounterType m_end;
};
CounterType m_start;
CounterType m_end;
CounterType m_resolution;
};
}
#endif //NEKTAR_LIB_UTILITIES_BASIC_UTILS_TIMER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment