Robotics Library  0.6.2
Timer.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef _RL_UTIL_TIMER_H_
28 #define _RL_UTIL_TIMER_H_
29 
30 #include <ctime>
31 
32 #ifdef WIN32
33 #include <windows.h>
34 #else // WIN32
35 #include <unistd.h>
36 #include <sys/time.h>
37 #endif // WIN32
38 
39 #include <rl/math/Unit.h>
40 
41 namespace rl
42 {
43  namespace util
44  {
45  class Timer
46  {
47  public:
48  explicit Timer() :
49  begin(),
50  end()
51  {
52 #ifdef WIN32
53  QueryPerformanceFrequency(&(*this).frequency);
54 #endif // WIN32
55  }
56 
57  virtual ~Timer()
58  {
59  }
60 
64  double elapsed() const
65  {
66 #ifdef WIN32
67  return static_cast< double >((*this).end.QuadPart - (*this).begin.QuadPart) / static_cast< double >((*this).frequency.QuadPart);
68 #else // WIN32
69  return static_cast< double >((*this).end.tv_sec - (*this).begin.tv_sec) + static_cast< double >((*this).end.tv_usec - (*this).begin.tv_usec) / 1000000.0;
70 #endif // WIN32
71  }
72 
76  static double now()
77  {
78 #ifdef WIN32
79  LARGE_INTEGER now;
80  QueryPerformanceCounter(&now);
81  LARGE_INTEGER frequency;
82  QueryPerformanceFrequency(&frequency);
83  return static_cast< double >(now.QuadPart) / static_cast< double >(frequency.QuadPart);
84 #else // WIN32
85  timeval now;
86  gettimeofday(&now, NULL);
87  return static_cast< double >(now.tv_sec) + static_cast< double >(now.tv_usec) / 1000000.0;
88 #endif // WIN32
89  }
90 
94  static void sleep(const double& seconds)
95  {
96 #ifdef WIN32
97  Sleep(static_cast< unsigned int >(seconds * ::rl::math::UNIT2MILLI));
98 #else // WIN32
99  usleep(static_cast< ::std::size_t >(seconds * ::rl::math::UNIT2MICRO));
100 #endif // WIN32
101  }
102 
103  void start()
104  {
105 #ifdef WIN32
106  QueryPerformanceCounter(&(*this).begin);
107 #else // WIN32
108  gettimeofday(&(*this).begin, NULL);
109 #endif // WIN32
110  }
111 
112  void stop()
113  {
114 #ifdef WIN32
115  QueryPerformanceCounter(&(*this).end);
116 #else // WIN32
117  gettimeofday(&(*this).end, NULL);
118 #endif // WIN32
119  }
120 
121  protected:
122 
123  private:
124 #ifdef WIN32
125  LARGE_INTEGER begin;
126 
127  LARGE_INTEGER end;
128 
129  LARGE_INTEGER frequency;
130 #else // WIN32
131  timeval begin;
132 
133  timeval end;
134 #endif // WIN32
135  };
136  }
137 }
138 
139 #endif // _RL_UTIL_TIMER_H_
rl::util::Timer::now
static double now()
Definition: Timer.h:76
rl::util::Timer::~Timer
virtual ~Timer()
Definition: Timer.h:57
rl::util::Timer::stop
void stop()
Definition: Timer.h:112
rl::util::Timer::start
void start()
Definition: Timer.h:103
rl::util::Timer::elapsed
double elapsed() const
Definition: Timer.h:64
rl::math::UNIT2MILLI
static const Real UNIT2MILLI
Definition: Unit.h:212
rl::util::Timer::sleep
static void sleep(const double &seconds)
Definition: Timer.h:94
rl::util::Timer::end
timeval end
Definition: Timer.h:133
rl::util::Timer::begin
timeval begin
Definition: Timer.h:131
rl::util::Timer::Timer
Timer()
Definition: Timer.h:48
rl::util::Timer
Definition: Timer.h:46
Unit.h
rl::math::UNIT2MICRO
static const Real UNIT2MICRO
Definition: Unit.h:210
rl
Definition: Ati.cpp:35