
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.45 KB | hits: 19 | expires: Never
// Timer.hpp
#ifndef _VR2_TIMER_HPP
#define _VR2_TIMER_HPP
#ifndef WIN32
#include <sys/time.h>
#include <time.h>
#include <stdint.h>
namespace utils
{
class Timer
{
timeval m_tic, m_toc;
public:
bool isValid();
void tic();
double toc();
};
#else
#include <iostream>
#include <windows.h>
namespace utils
{
class Timer
{
static LARGE_INTEGER m_timerResolution;
static bool m_valid;
LARGE_INTEGER m_tic, m_toc;
public:
Timer()
{
//std::cerr << "Timer resolution: " << m_timerResolution.QuadPart << "Hz." << std::endl;
}
bool isValid();
void tic();
double toc();
};
#endif
}
#endif
// Timer.cpp
#include "Timer.hpp"
namespace utils
{
#ifndef WIN32
bool Timer::isValid()
{
return true;
}
void Timer::tic()
{
gettimeofday(&m_tic, NULL);
}
double Timer::toc()
{
gettimeofday(&m_toc, NULL);
uint64_t us = ((m_toc.tv_sec * 1000000) + m_toc.tv_usec) -
((m_tic.tv_sec * 1000000) + m_tic.tv_usec);
return (double)us/1000000.0;
}
#else
LARGE_INTEGER Timer::m_timerResolution;
bool Timer::m_valid = (0 != QueryPerformanceFrequency(&m_timerResolution));
bool Timer::isValid()
{
return m_valid;
}
void Timer::tic()
{
QueryPerformanceCounter(&m_tic);
}
double Timer::toc()
{
QueryPerformanceCounter(&m_toc);
return (m_toc.QuadPart - m_tic.QuadPart)/(double)m_timerResolution.QuadPart;
}
#endif
}