Commit b78bc93f authored by Douglas Serson's avatar Douglas Serson

Use chrono to simplify timer

parent b149b3a1
...@@ -37,91 +37,35 @@ ...@@ -37,91 +37,35 @@
namespace Nektar namespace Nektar
{ {
Timer::Timer() :
m_start(),
m_end(),
m_resolution()
{
}
Timer::~Timer() Timer::Timer() :
{ m_start(),
} m_end()
{
void Timer::Start() }
{
#ifdef _WIN32
QueryPerformanceCounter(&m_start);
#elif defined(__APPLE__)
gettimeofday(&m_start, 0);
#else
clock_gettime(CLOCK_REALTIME, &m_start);
#endif
}
void Timer::Stop() Timer::~Timer()
{ {
#ifdef _WIN32 }
QueryPerformanceCounter(&m_end);
#elif defined(__APPLE__)
gettimeofday(&m_end, 0);
#else
clock_gettime(CLOCK_REALTIME, &m_end);
#endif
}
Timer::CounterType Timer::Elapsed() void Timer::Start()
{ {
#ifdef _WIN32 m_start = Clock::now();
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;
if( result.tv_nsec < m_start.tv_nsec) void Timer::Stop()
{ {
result.tv_sec -= 1; m_end = Clock::now();
result.tv_nsec += 1000000000; }
}
result.tv_sec -= m_start.tv_sec;
result.tv_nsec -= m_start.tv_nsec;
return result; Timer::Seconds Timer::Elapsed()
#endif {
} return std::chrono::duration_cast<Seconds>(m_end - m_start);
}
NekDouble Timer::TimePerTest(unsigned int n) NekDouble Timer::TimePerTest(unsigned int n)
{ {
#ifdef _WIN32 return Elapsed().count() / static_cast<NekDouble>(n);
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
}
} }
...@@ -37,50 +37,41 @@ ...@@ -37,50 +37,41 @@
#ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_TIMER_H #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_TIMER_H
#define NEKTAR_LIB_UTILITIES_BASIC_UTILS_TIMER_H #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_TIMER_H
#ifdef _WIN32 #include <chrono>
#define NOMINMAX
#include <windows.h>
#else
#include <sys/time.h>
#include <time.h>
#endif
#include <LibUtilities/LibUtilitiesDeclspec.h> #include <LibUtilities/LibUtilitiesDeclspec.h>
#include <LibUtilities/BasicConst/NektarUnivConsts.hpp> #include <LibUtilities/BasicConst/NektarUnivConsts.hpp>
namespace Nektar namespace Nektar
{ {
class Timer
{
public:
#ifdef _WIN32
typedef LARGE_INTEGER CounterType;
#elif defined(__APPLE__)
typedef timeval CounterType;
#else
typedef timespec CounterType;
#endif
public: class Timer
LIB_UTILITIES_EXPORT Timer(); {
LIB_UTILITIES_EXPORT ~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 Start();
LIB_UTILITIES_EXPORT void Stop(); LIB_UTILITIES_EXPORT void Stop();
LIB_UTILITIES_EXPORT CounterType Elapsed(); LIB_UTILITIES_EXPORT Seconds Elapsed();
/// \brief Returns amount of seconds per iteration in /// \brief Returns amount of seconds per iteration in
/// a test with n iterations. /// a test with n iterations.
LIB_UTILITIES_EXPORT NekDouble TimePerTest(unsigned int n); LIB_UTILITIES_EXPORT NekDouble TimePerTest(unsigned int n);
private: private:
Timer(const Timer& rhs); CounterType m_start;
Timer& operator=(const Timer& rhs); CounterType m_end;
};
CounterType m_start;
CounterType m_end;
CounterType m_resolution;
};
} }
#endif //NEKTAR_LIB_UTILITIES_BASIC_UTILS_TIMER_H #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