2 #ifndef OPENGM_TIMER_HXX
3 #define OPENGM_TIMER_HXX
7 # if (defined(_OPENGM_TIMER_MACH__) || defined(__APPLE__))
8 # define OPENGM_TIMER_MAC
9 # elif (defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(_WIN64))
10 # define OPENGM_TIMER_WINDOWS
11 # ifndef WIN32_LEAN_AND_MEAN
12 # define WIN32_LEAN_AND_MEAN
16 # if defined(OPENGM_TIMER_MAC)
17 # include <mach/mach_time.h>
18 # elif defined(OPENGM_TIMER_WINDOWS)
43 #if defined(OPENGM_TIMER_MAC)
44 typedef uint64_t TimerT;
45 typedef double TimerC;
46 #elif defined(OPENGM_TIMER_WINDOWS)
47 typedef LONGLONG TimerT;
48 typedef LARGE_INTEGER TimerC;
50 typedef double TimerT;
51 typedef timespec TimerC;
55 #if !defined(OPENGM_TIMER_MAC)
59 double conversionFactor_;
64 template<
class FUNCTOR>
69 Timing(Functor,
const size_t = 1);
70 const std::vector<double>&
times()
const;
74 std::vector<double> times_;
78 : start_(0), duration_(0), elapsedTime_(0)
80 #if defined(OPENGM_TIMER_MAC)
81 mach_timebase_info_data_t info;
82 mach_timebase_info(&info);
83 conversionFactor_ = (
static_cast<double>(info.numer))/
84 (
static_cast<double>(info.denom));
85 conversionFactor_ = conversionFactor_*1.0e-9;
86 #elif defined(OPENGM_TIMER_WINDOWS)
88 QueryPerformanceFrequency(&freq);
89 conversionFactor_ = 1.0/(
static_cast<double>(freq.QuadPart));
91 conversionFactor_ = 1.0;
97 #if defined(OPENGM_TIMER_MAC)
98 start_ = mach_absolute_time();
99 #elif defined(OPENGM_TIMER_WINDOWS)
100 QueryPerformanceCounter(&ts_);
101 start_ = ts_.QuadPart;
103 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_);
104 start_ =
static_cast<double>(ts_.tv_sec) + 1.0e-9 *
105 static_cast<double>(ts_.tv_nsec);
110 #if defined(OPENGM_TIMER_MAC)
111 duration_ =
static_cast<double>(mach_absolute_time() - start_);
112 #elif defined(OPENGM_TIMER_WINDOWS)
114 QueryPerformanceCounter(&qpc_t);
115 duration_ =
static_cast<double>(qpc_t.QuadPart - start_);
117 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_);
118 duration_ = (
static_cast<double>(ts_.tv_sec) + 1.0e-9 *
119 static_cast<double>(ts_.tv_nsec)) - start_;
121 elapsedTime_ = duration_*conversionFactor_;
134 template<
class FUNCTOR>
137 const size_t repetitions
140 times_(
std::vector<double>())
142 if(repetitions < 1) {
143 throw std::runtime_error(
"The number of repetition must be at least 1.");
145 for(
size_t j=0; j<repetitions; ++j) {
154 template<
class FUNCTOR>
155 inline const std::vector<double>&
162 #if defined(OPENGM_TIMER_WINDOWS)
163 # undef WIN32_LEAN_AND_MEAN
166 #endif // OPENGM_TIMER_HXX
Timing(Functor, const size_t=1)
Platform-independent runtime measurements.
const std::vector< double > & times() const
Platform-independent runtime measurements of functors.
double elapsedTime() const