b2_timer.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "box2d/b2_timer.h"
24 
25 #if defined(_WIN32)
26 
27 double b2Timer::s_invFrequency = 0.0;
28 
29 #ifndef WIN32_LEAN_AND_MEAN
30 #define WIN32_LEAN_AND_MEAN
31 #endif
32 
33 #include <windows.h>
34 
36 {
37  LARGE_INTEGER largeInteger;
38 
39  if (s_invFrequency == 0.0)
40  {
41  QueryPerformanceFrequency(&largeInteger);
42  s_invFrequency = double(largeInteger.QuadPart);
43  if (s_invFrequency > 0.0)
44  {
45  s_invFrequency = 1000.0 / s_invFrequency;
46  }
47  }
48 
49  QueryPerformanceCounter(&largeInteger);
50  m_start = double(largeInteger.QuadPart);
51 }
52 
53 void b2Timer::Reset()
54 {
55  LARGE_INTEGER largeInteger;
56  QueryPerformanceCounter(&largeInteger);
57  m_start = double(largeInteger.QuadPart);
58 }
59 
60 float b2Timer::GetMilliseconds() const
61 {
62  LARGE_INTEGER largeInteger;
63  QueryPerformanceCounter(&largeInteger);
64  double count = double(largeInteger.QuadPart);
65  float ms = float(s_invFrequency * (count - m_start));
66  return ms;
67 }
68 
69 #elif defined(__linux__) || defined (__APPLE__)
70 
71 #include <sys/time.h>
72 
74 {
75  Reset();
76 }
77 
78 void b2Timer::Reset()
79 {
80  timeval t;
81  gettimeofday(&t, 0);
82  m_start_sec = t.tv_sec;
83  m_start_usec = t.tv_usec;
84 }
85 
86 float b2Timer::GetMilliseconds() const
87 {
88  timeval t;
89  gettimeofday(&t, 0);
90  time_t start_sec = m_start_sec;
91  suseconds_t start_usec = m_start_usec;
92 
93  // http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html
94  if (t.tv_usec < start_usec)
95  {
96  int nsec = (start_usec - t.tv_usec) / 1000000 + 1;
97  start_usec -= 1000000 * nsec;
98  start_sec += nsec;
99  }
100 
101  if (t.tv_usec - start_usec > 1000000)
102  {
103  int nsec = (t.tv_usec - start_usec) / 1000000;
104  start_usec += 1000000 * nsec;
105  start_sec -= nsec;
106  }
107  return 1000.0f * (t.tv_sec - start_sec) + 0.001f * (t.tv_usec - start_usec);
108 }
109 
110 #else
111 
113 {
114 }
115 
117 {
118 }
119 
121 {
122  return 0.0f;
123 }
124 
125 #endif
geometry_msgs::TransformStamped t
b2Timer()
Constructor.
Definition: b2_timer.cpp:112
void Reset()
Reset the timer.
Definition: b2_timer.cpp:116
float GetMilliseconds() const
Get the time since construction or the last reset.
Definition: b2_timer.cpp:120


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19