Main MRPT website > C++ reference for MRPT 1.9.9
CTicTac.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/config.h>
13 
14 #ifdef MRPT_OS_WINDOWS
15 #include <windows.h>
16 #include <mrpt/utils/utils_defs.h>
17 #else
18 #include <sys/time.h>
19 #endif
20 
21 #include <mrpt/utils/CTicTac.h>
22 #include <cstring>
23 
24 // For Windows: get the common code out of CTicTac so it's only run once!
25 #ifdef MRPT_OS_WINDOWS
26 struct AuxWindowsTicTac
27 {
28  static AuxWindowsTicTac& GetInstance()
29  {
30  static AuxWindowsTicTac obj;
31  return obj;
32  }
33 
34  double dbl_period;
35 
36  private:
37  LARGE_INTEGER m_freq;
38 
39  AuxWindowsTicTac() : dbl_period(.0)
40  {
41  QueryPerformanceFrequency(&m_freq);
42  ASSERTMSG_(
43  m_freq.QuadPart != 0, "Error getting QueryPerformanceFrequency()");
44  dbl_period = 1.0 / static_cast<double>(m_freq.QuadPart);
45  }
46 };
47 
48 #endif
49 
50 using namespace mrpt::utils;
51 
52 // Macros for easy access to memory with the correct types:
53 #ifdef MRPT_OS_WINDOWS
54 #define LARGE_INTEGER_NUMS reinterpret_cast<LARGE_INTEGER*>(largeInts)
55 #else
56 #define TIMEVAL_NUMS reinterpret_cast<struct timeval*>(largeInts)
57 #endif
58 
59 /*---------------------------------------------------------------
60  Constructor
61  ---------------------------------------------------------------*/
63 {
64  ::memset(largeInts, 0, sizeof(largeInts));
65 
66 #ifdef MRPT_OS_WINDOWS
67  static_assert(
68  sizeof(largeInts) >= 2 * sizeof(LARGE_INTEGER),
69  "sizeof(LARGE_INTEGER) failed!");
70 #else
71  static_assert(
72  sizeof(largeInts) > 2 * sizeof(struct timeval),
73  "sizeof(struct timeval) failed!");
74 #endif
75  Tic();
76 }
77 
78 /*---------------------------------------------------------------
79  Tic
80  Starts the stopwatch
81  ---------------------------------------------------------------*/
83 {
84 #ifdef MRPT_OS_WINDOWS
85  LARGE_INTEGER* l = LARGE_INTEGER_NUMS;
86  QueryPerformanceCounter(&l[0]);
87 #else
88  struct timeval* ts = TIMEVAL_NUMS;
89  gettimeofday(&ts[0], nullptr);
90 #endif
91 }
92 
93 /*---------------------------------------------------------------
94  Tac
95  Stop. Returns ellapsed time in seconds
96  ---------------------------------------------------------------*/
97 double CTicTac::Tac()
98 {
99 #ifdef MRPT_OS_WINDOWS
100  LARGE_INTEGER* l = LARGE_INTEGER_NUMS;
101  QueryPerformanceCounter(&l[1]);
102  return (l[1].QuadPart - l[0].QuadPart) *
103  AuxWindowsTicTac::GetInstance().dbl_period;
104 #else
105  struct timeval* ts = TIMEVAL_NUMS;
106  gettimeofday(&ts[1], nullptr);
107  return (ts[1].tv_sec - ts[0].tv_sec) +
108  1e-6 * (ts[1].tv_usec - ts[0].tv_usec);
109 #endif
110 }
unsigned char largeInts[64]
Definition: CTicTac.h:37
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
void Tic()
Starts the stopwatch.
Definition: CTicTac.cpp:82
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
CTicTac()
Default constructor.
Definition: CTicTac.cpp:62
#define TIMEVAL_NUMS
Definition: CTicTac.cpp:56
double Tac()
Stops the stopwatch.
Definition: CTicTac.cpp:97
#define ASSERTMSG_(f, __ERROR_MSG)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019