UTimer.cpp
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "rtabmap/utilite/UTimer.h"
22 
24 // public:
27 {
28 #ifdef _WIN32
29  QueryPerformanceFrequency(&frequency_);
30 #endif
31  start(); // This will initialize the private counters
32 }
33 
35 
36 #ifdef _WIN32
37 double UTimer::now()
38 {
39 #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
40  FILETIME ft;
41  GetSystemTimePreciseAsFileTime(&ft);
42  __int64* val = (__int64*)&ft;
43  return static_cast<double>(*val) / 10000000.0 - 11644473600.0; // The Windows epoch is Jan 1 1601, the Unix epoch Jan 1 1970.
44 #else
45  LARGE_INTEGER count, freq;
46  QueryPerformanceFrequency(&freq);
47  QueryPerformanceCounter(&count);
48  return double(count.QuadPart) / freq.QuadPart;
49 #endif
50 }
51 
52 void UTimer::start()
53 {
54  QueryPerformanceCounter(&startTimeRecorded_);
56 }
57 void UTimer::stop()
58 {
59  QueryPerformanceCounter(&stopTimeRecorded_);
60 
61 }
63 {
64  LARGE_INTEGER now;
65  QueryPerformanceCounter(&now);
66  return double(now.QuadPart - startTimeRecorded_.QuadPart) / frequency_.QuadPart;
67 }
68 double UTimer::getInterval()
69 {
70  if(stopTimeRecorded_.QuadPart == startTimeRecorded_.QuadPart)
71  {
72  return getElapsedTime();
73  }
74  else
75  {
76  return double(stopTimeRecorded_.QuadPart - startTimeRecorded_.QuadPart) / frequency_.QuadPart;
77  }
78 }
79 #else
80 double UTimer::now()
81 {
82  struct timeval tv;
83  gettimeofday(&tv, NULL);
84  return double(tv.tv_sec) + double(tv.tv_usec) / 1000000.0;
85 }
86 
88 {
89  gettimeofday(&startTimeRecorded_, NULL);
91 }
93 {
94  gettimeofday(&stopTimeRecorded_, NULL);
95 
96 }
98 {
99  return UTimer::now() - (double(startTimeRecorded_.tv_sec) + double(startTimeRecorded_.tv_usec) / 1000000.0);
100 
101 }
102 double UTimer::getInterval()
103 {
104  if(startTimeRecorded_.tv_sec == stopTimeRecorded_.tv_sec && startTimeRecorded_.tv_usec == stopTimeRecorded_.tv_usec)
105  {
106  return getElapsedTime();
107  }
108  else
109  {
110  double start = double(startTimeRecorded_.tv_sec) + double(startTimeRecorded_.tv_usec) / 1000000.0;
111  double stop = double(stopTimeRecorded_.tv_sec) + double(stopTimeRecorded_.tv_usec) / 1000000.0;
112  return stop - start;
113  }
114 }
115 #endif
116 
117 double UTimer::ticks() // Stop->start and return Interval
118 {
119  double inter = elapsed();
120  start();
121  return inter;
122 }
#define NULL
double elapsed()
Definition: UTimer.h:75
UTimer()
Definition: UTimer.cpp:26
struct timeval stopTimeRecorded_
Definition: UTimer.h:105
struct timeval startTimeRecorded_
Definition: UTimer.h:104
double getElapsedTime()
Definition: UTimer.cpp:97
~UTimer()
Definition: UTimer.cpp:34
void stop()
Definition: UTimer.cpp:92
void start()
Definition: UTimer.cpp:87
ULogger class and convenient macros.
double ticks()
Definition: UTimer.cpp:117
static double now()
Definition: UTimer.cpp:80


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:38:58