Robotics Library  0.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 // * Neither the name of the Technische Universitaet Muenchen nor the names of
14 // its contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef _RL_UTIL_TIMER_H_
31 #define _RL_UTIL_TIMER_H_
32 
33 #include <ctime>
34 
35 #ifdef WIN32
36 #include <windows.h>
37 #else // WIN32
38 #include <unistd.h>
39 #include <sys/time.h>
40 #endif // WIN32
41 
42 namespace rl
43 {
44  namespace util
45  {
46  class Timer
47  {
48  public:
49  explicit Timer() :
50  begin(),
51  end()
52  {
53 #ifdef WIN32
54  QueryPerformanceFrequency(&(*this).frequency);
55 #endif // WIN32
56  }
57 
58  virtual ~Timer()
59  {
60  }
61 
65  double elapsed() const
66  {
67 #ifdef WIN32
68  return static_cast< double >((*this).end.QuadPart - (*this).begin.QuadPart) / static_cast< double >((*this).frequency.QuadPart);
69 #else // WIN32
70  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;
71 #endif // WIN32
72  }
73 
77  static double now()
78  {
79 #ifdef WIN32
80  LARGE_INTEGER now;
81  QueryPerformanceCounter(&now);
82  LARGE_INTEGER frequency;
83  QueryPerformanceFrequency(&frequency);
84  return static_cast< double >(now.QuadPart) / static_cast< double >(frequency.QuadPart);
85 #else // WIN32
86  timeval now;
87  gettimeofday(&now, NULL);
88  return static_cast< double >(now.tv_sec) + static_cast< double >(now.tv_usec) / 1000000.0;
89 #endif // WIN32
90  }
91 
95  static void sleep(const double& seconds)
96  {
97 #ifdef WIN32
98  Sleep(static_cast< unsigned int >(seconds * 1000.0));
99 #else // WIN32
100  usleep(static_cast< ::std::size_t >(seconds * 1000.0 * 1000.0));
101 #endif // WIN32
102  }
103 
104  void start()
105  {
106 #ifdef WIN32
107  QueryPerformanceCounter(&(*this).begin);
108 #else // WIN32
109  gettimeofday(&(*this).begin, NULL);
110 #endif // WIN32
111  }
112 
113  void stop()
114  {
115 #ifdef WIN32
116  QueryPerformanceCounter(&(*this).end);
117 #else // WIN32
118  gettimeofday(&(*this).end, NULL);
119 #endif // WIN32
120  }
121 
122  protected:
123 
124  private:
125 #ifdef WIN32
126  LARGE_INTEGER begin;
127 
128  LARGE_INTEGER end;
129 
130  LARGE_INTEGER frequency;
131 #else // WIN32
132  timeval begin;
133 
134  timeval end;
135 #endif // WIN32
136  };
137  }
138 }
139 
140 #endif // _RL_UTIL_TIMER_H_