Main MRPT website > C++ reference for MRPT 1.5.6
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_(m_freq.QuadPart != 0, "Error getting QueryPerformanceFrequency()");
43  dbl_period = 1.0 / static_cast<double>(m_freq.QuadPart);
44  }
45 };
46 
47 #endif
48 
49 using namespace mrpt::utils;
50 
51 // Macros for easy access to memory with the correct types:
52 #ifdef MRPT_OS_WINDOWS
53 # define LARGE_INTEGER_NUMS reinterpret_cast<LARGE_INTEGER*>(largeInts)
54 #else
55 # define TIMEVAL_NUMS reinterpret_cast<struct timeval*>(largeInts)
56 #endif
57 
58 /*---------------------------------------------------------------
59  Constructor
60  ---------------------------------------------------------------*/
62 {
63  ::memset( largeInts, 0, sizeof(largeInts) );
64 
65 #ifdef MRPT_OS_WINDOWS
66  static_assert( sizeof( largeInts ) >= 2*sizeof(LARGE_INTEGER), "sizeof(LARGE_INTEGER) failed!");
67 #else
68  static_assert( sizeof( largeInts ) > 2*sizeof(struct timeval), "sizeof(struct timeval) failed!");
69 #endif
70  Tic();
71 }
72 
73 /*---------------------------------------------------------------
74  Tic
75  Starts the stopwatch
76  ---------------------------------------------------------------*/
78 {
79 #ifdef MRPT_OS_WINDOWS
80  LARGE_INTEGER *l= LARGE_INTEGER_NUMS;
81  QueryPerformanceCounter(&l[0]);
82 #else
83  struct timeval* ts = TIMEVAL_NUMS;
84  gettimeofday( &ts[0], NULL);
85 #endif
86 }
87 
88 /*---------------------------------------------------------------
89  Tac
90  Stop. Returns ellapsed time in seconds
91  ---------------------------------------------------------------*/
92 double CTicTac::Tac()
93 {
94 #ifdef MRPT_OS_WINDOWS
95  LARGE_INTEGER *l= LARGE_INTEGER_NUMS;
96  QueryPerformanceCounter( &l[1] );
97  return (l[1].QuadPart-l[0].QuadPart) * AuxWindowsTicTac::GetInstance().dbl_period;
98 #else
99  struct timeval* ts = TIMEVAL_NUMS;
100  gettimeofday( &ts[1], NULL);
101  return ( ts[1].tv_sec - ts[0].tv_sec) + 1e-6*( ts[1].tv_usec - ts[0].tv_usec );
102 #endif
103 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
unsigned long long largeInts[8]
Definition: CTicTac.h:31
void Tic()
Starts the stopwatch.
Definition: CTicTac.cpp:77
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
CTicTac()
Default constructor. Implicitly calls Tic()
Definition: CTicTac.cpp:61
#define TIMEVAL_NUMS
Definition: CTicTac.cpp:55
double Tac()
Stops the stopwatch.
Definition: CTicTac.cpp:92
#define ASSERTMSG_(f, __ERROR_MSG)



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019