BenchTimer backport (clock_gettime & QueryPerformanceCounter).

This commit is contained in:
Hauke Heibel 2010-02-03 21:55:01 +01:00
parent a9eabed421
commit 743ad75595

View File

@ -1,8 +1,8 @@
// This file is part of Eigen, a lightweight C++ template library // This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project. // for linear algebra.
// //
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com> // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
// //
// Eigen is free software; you can redistribute it and/or // Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public // modify it under the terms of the GNU Lesser General Public
@ -26,8 +26,15 @@
#ifndef EIGEN_BENCH_TIMER_H #ifndef EIGEN_BENCH_TIMER_H
#define EIGEN_BENCH_TIMER_H #define EIGEN_BENCH_TIMER_H
#include <sys/time.h> #if defined(_WIN32) || defined(__CYGWIN__)
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <time.h>
#include <unistd.h> #include <unistd.h>
#endif
#include <cstdlib> #include <cstdlib>
#include <numeric> #include <numeric>
@ -35,12 +42,25 @@ namespace Eigen
{ {
/** Elapsed time timer keeping the best try. /** Elapsed time timer keeping the best try.
*
* On POSIX platforms we use clock_gettime with CLOCK_PROCESS_CPUTIME_ID.
* On Windows we use QueryPerformanceCounter
*
* Important: on linux, you must link with -lrt
*/ */
class BenchTimer class BenchTimer
{ {
public: public:
BenchTimer() { reset(); } BenchTimer()
{
#if defined(_WIN32) || defined(__CYGWIN__)
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
m_frequency = (double)freq.QuadPart;
#endif
reset();
}
~BenchTimer() {} ~BenchTimer() {}
@ -51,23 +71,34 @@ public:
m_best = std::min(m_best, getTime() - m_start); m_best = std::min(m_best, getTime() - m_start);
} }
/** Return the best elapsed time. /** Return the best elapsed time in seconds.
*/ */
inline double value(void) inline double value(void)
{ {
return m_best; return m_best;
} }
#if defined(_WIN32) || defined(__CYGWIN__)
inline double getTime(void)
#else
static inline double getTime(void) static inline double getTime(void)
#endif
{ {
struct timeval tv; #ifdef WIN32
struct timezone tz; LARGE_INTEGER query_ticks;
gettimeofday(&tv, &tz); QueryPerformanceCounter(&query_ticks);
return (double)tv.tv_sec + 1.e-6 * (double)tv.tv_usec; return query_ticks.QuadPart/m_frequency;
#else
timespec ts;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
return double(ts.tv_sec) + 1e-9 * double(ts.tv_nsec);
#endif
} }
protected: protected:
#if defined(_WIN32) || defined(__CYGWIN__)
double m_frequency;
#endif
double m_best, m_start; double m_best, m_start;
}; };