#if !defined(HIGH_RESOLUTION_TIMER_MAR_24_2008_1222PM)
#define HIGH_RESOLUTION_TIMER_MAR_24_2008_1222PM
#include <boost/config.hpp>
#include <boost/throw_exception.hpp>
#if defined(BOOST_HAS_UNISTD_H)
#include <unistd.h>
#endif
#include <time.h>
#include <stdexcept>
#include <limits>
#if defined(BOOST_WINDOWS)
#include <windows.h>
namespace boost {
namespace archive {
namespace xml {
class high_resolution_timer
{
public:
high_resolution_timer()
{
restart();
}
high_resolution_timer(double t)
{
LARGE_INTEGER frequency;
if (!QueryPerformanceFrequency(&frequency))
boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
start_time.QuadPart = (LONGLONG)(t * frequency.QuadPart);
}
high_resolution_timer(high_resolution_timer const& rhs)
: start_time(rhs.start_time)
{
}
static double now()
{
SYSTEMTIME st;
GetSystemTime(&st);
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
LARGE_INTEGER now;
now.LowPart = ft.dwLowDateTime;
now.HighPart = ft.dwHighDateTime;
return now.QuadPart * 1e-7;
}
void restart()
{
if (!QueryPerformanceCounter(&start_time))
boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
}
double elapsed() const {
LARGE_INTEGER now;
if (!QueryPerformanceCounter(&now))
boost::throw_exception(std::runtime_error("Couldn't get current time"));
LARGE_INTEGER frequency;
if (!QueryPerformanceFrequency(&frequency))
boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
return double(now.QuadPart - start_time.QuadPart) / frequency.QuadPart;
}
double elapsed_max() const {
LARGE_INTEGER frequency;
if (!QueryPerformanceFrequency(&frequency))
boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
return double((std::numeric_limits<LONGLONG>::max)() - start_time.QuadPart) /
double(frequency.QuadPart);
}
double elapsed_min() const {
LARGE_INTEGER frequency;
if (!QueryPerformanceFrequency(&frequency))
boost::throw_exception(std::runtime_error("Couldn't acquire frequency"));
return 1.0 / frequency.QuadPart;
}
private:
LARGE_INTEGER start_time;
};
} } }
#elif defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(_POSIX_THREAD_CPUTIME)
#if _POSIX_THREAD_CPUTIME > 0
namespace boost {
namespace archive {
namespace xml {
class high_resolution_timer
{
public:
high_resolution_timer()
{
start_time.tv_sec = 0;
start_time.tv_nsec = 0;
restart();
}
high_resolution_timer(double t)
{
start_time.tv_sec = time_t(t);
start_time.tv_nsec = (t - start_time.tv_sec) * 1e9;
}
high_resolution_timer(high_resolution_timer const& rhs)
: start_time(rhs.start_time)
{
}
static double now()
{
timespec now;
if (-1 == clock_gettime(CLOCK_REALTIME, &now))
boost::throw_exception(std::runtime_error("Couldn't get current time"));
return double(now.tv_sec) + double(now.tv_nsec) * 1e-9;
}
void restart()
{
if (-1 == clock_gettime(CLOCK_REALTIME, &start_time))
boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
}
double elapsed() const {
timespec now;
if (-1 == clock_gettime(CLOCK_REALTIME, &now))
boost::throw_exception(std::runtime_error("Couldn't get current time"));
if (now.tv_sec == start_time.tv_sec)
return double(now.tv_nsec - start_time.tv_nsec) * 1e-9;
return double(now.tv_sec - start_time.tv_sec) +
(double(now.tv_nsec - start_time.tv_nsec) * 1e-9);
}
double elapsed_max() const {
return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
}
double elapsed_min() const {
timespec resolution;
if (-1 == clock_getres(CLOCK_REALTIME, &resolution))
boost::throw_exception(std::runtime_error("Couldn't get resolution"));
return double(resolution.tv_sec + resolution.tv_nsec * 1e-9);
}
private:
timespec start_time;
};
} } }
#else
#include <boost/timer.hpp>
namespace boost {
namespace archive {
namespace xml {
class high_resolution_timer
{
public:
high_resolution_timer()
: use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0)
{
if (!use_backup) {
start_time.tv_sec = 0;
start_time.tv_nsec = 0;
}
restart();
}
high_resolution_timer(double t)
: use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0)
{
if (!use_backup) {
start_time.tv_sec = time_t(t);
start_time.tv_nsec = (t - start_time.tv_sec) * 1e9;
}
}
high_resolution_timer(high_resolution_timer const& rhs)
: use_backup(sysconf(_SC_THREAD_CPUTIME) <= 0),
start_time(rhs.start_time)
{
}
static double now()
{
if (sysconf(_SC_THREAD_CPUTIME) <= 0)
return double(std::clock());
timespec now;
if (-1 == clock_gettime(CLOCK_REALTIME, &now))
boost::throw_exception(std::runtime_error("Couldn't get current time"));
return double(now.tv_sec) + double(now.tv_nsec) * 1e-9;
}
void restart()
{
if (use_backup)
start_time_backup.restart();
else if (-1 == clock_gettime(CLOCK_REALTIME, &start_time))
boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
}
double elapsed() const {
if (use_backup)
return start_time_backup.elapsed();
timespec now;
if (-1 == clock_gettime(CLOCK_REALTIME, &now))
boost::throw_exception(std::runtime_error("Couldn't get current time"));
if (now.tv_sec == start_time.tv_sec)
return double(now.tv_nsec - start_time.tv_nsec) * 1e-9;
return double(now.tv_sec - start_time.tv_sec) +
(double(now.tv_nsec - start_time.tv_nsec) * 1e-9);
}
double elapsed_max() const {
if (use_backup)
start_time_backup.elapsed_max();
return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
}
double elapsed_min() const {
if (use_backup)
start_time_backup.elapsed_min();
timespec resolution;
if (-1 == clock_getres(CLOCK_REALTIME, &resolution))
boost::throw_exception(std::runtime_error("Couldn't get resolution"));
return double(resolution.tv_sec + resolution.tv_nsec * 1e-9);
}
private:
bool use_backup;
timespec start_time;
boost::timer start_time_backup;
};
} } }
#endif
#else
#if defined(BOOST_HAS_GETTIMEOFDAY)
#include <sys/time.h>
namespace boost {
namespace archive {
namespace xml {
class high_resolution_timer
{
private:
template <typename U>
static inline double unsigned_diff(const U &a, const U &b)
{
if (a > b)
return static_cast<double>(a-b);
return -static_cast<double>(b-a);
}
double elapsed(const timeval &t1, const timeval &t0) const
{
if (t1.tv_sec == t0.tv_sec)
return unsigned_diff(t1.tv_usec,t0.tv_usec) * 1e-6;
return unsigned_diff(t1.tv_sec,t0.tv_sec) +
unsigned_diff(t1.tv_usec,t0.tv_usec) * 1e-6;
}
public:
high_resolution_timer()
{
start_time.tv_sec = 0;
start_time.tv_usec = 0;
restart();
}
high_resolution_timer(double t)
{
start_time.tv_sec = time_t(t);
start_time.tv_usec = (t - start_time.tv_sec) * 1e6;
}
high_resolution_timer(high_resolution_timer const& rhs)
: start_time(rhs.start_time)
{
}
static double now()
{
timeval now;
if (gettimeofday(&now, NULL))
boost::throw_exception(std::runtime_error("Couldn't get current time"));
return double(now.tv_sec) + double(now.tv_usec) * 1e-6;
}
void restart()
{
if (gettimeofday(&start_time, NULL))
boost::throw_exception(std::runtime_error("Couldn't initialize start_time"));
}
double elapsed() const {
timeval now;
if (gettimeofday(&now, NULL))
boost::throw_exception(std::runtime_error("Couldn't get current time"));
return elapsed(now,start_time);
}
double elapsed_max() const {
return double((std::numeric_limits<time_t>::max)() - start_time.tv_sec);
}
double elapsed_min() const {
timeval t0, t1;
double delta(0);
if (gettimeofday(&t0, NULL))
boost::throw_exception(std::runtime_error("Couldn't get resolution."));
do {
if (gettimeofday(&t1, NULL))
boost::throw_exception(std::runtime_error("Couldn't get resolution."));
delta = elapsed(t1, t0);
} while (delta <= 0.0);
return delta;
}
private:
timeval start_time;
};
} } }
#else
#include <boost/timer.hpp>
namespace boost {
namespace archive {
namespace xml {
struct high_resolution_timer
: boost::timer
{
static double now()
{
return double(std::clock());
}
};
} } }
#endif
#endif
#endif