MRPT  2.0.4
CTicTac.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #ifdef _WIN32
13 #include <windows.h>
14 #else
15 #include <sys/time.h>
16 #include <time.h>
17 #endif
18 
19 #include <mrpt/core/exceptions.h>
20 #include <mrpt/system/CTicTac.h>
21 #include <cassert>
22 #include <cstring>
23 
24 #include <type_traits>
25 
26 // For Windows: get the common code out of CTicTac so it's only run once!
27 #ifdef _WIN32
29 {
30  static AuxWindowsTicTac& GetInstance() noexcept
31  {
32  static AuxWindowsTicTac obj;
33  return obj;
34  }
35 
36  double dbl_period;
37 
38  private:
39  LARGE_INTEGER m_freq;
40 
41  AuxWindowsTicTac() noexcept : dbl_period(.0)
42  {
43  QueryPerformanceFrequency(&m_freq);
44  assert(m_freq.QuadPart != 0);
45  dbl_period = 1.0 / static_cast<double>(m_freq.QuadPart);
46  }
47 };
48 
49 #endif
50 
51 using namespace mrpt::system;
52 
53 // Macros for easy access to memory with the correct types:
54 #ifdef _WIN32
55 #define LARGE_INTEGER_NUMS reinterpret_cast<LARGE_INTEGER*>(largeInts)
56 #else
57 #define TIMEVAL_NUMS reinterpret_cast<struct timespec*>(largeInts)
58 #endif
59 
60 CTicTac::CTicTac() noexcept
61 {
62  ::memset(largeInts, 0, sizeof(largeInts));
63 
64 #ifdef _WIN32
65  static_assert(
66  sizeof(largeInts) >= 2 * sizeof(LARGE_INTEGER),
67  "sizeof(LARGE_INTEGER) failed!");
68 #else
69  static_assert(
70  sizeof(largeInts) >= 2 * sizeof(struct timespec),
71  "sizeof(struct timespec) failed!");
72 #endif
73  Tic();
74 }
75 
76 void CTicTac::Tic() noexcept
77 {
78 #ifdef _WIN32
79  LARGE_INTEGER* l = LARGE_INTEGER_NUMS;
80  QueryPerformanceCounter(&l[0]);
81 #else
82  auto* ts = TIMEVAL_NUMS;
83  clock_gettime(CLOCK_MONOTONIC, &ts[0]);
84 #endif
85 }
86 
87 double CTicTac::Tac() noexcept
88 {
89 #ifdef _WIN32
90  LARGE_INTEGER* l = LARGE_INTEGER_NUMS;
91  QueryPerformanceCounter(&l[1]);
92  return (l[1].QuadPart - l[0].QuadPart) *
94 #else
95  auto* ts = TIMEVAL_NUMS;
96  clock_gettime(CLOCK_MONOTONIC, &ts[1]);
97  return static_cast<double>(ts[1].tv_sec - ts[0].tv_sec) +
98  1e-9 * static_cast<double>(ts[1].tv_nsec - ts[0].tv_nsec);
99 #endif
100 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:87
CTicTac() noexcept
Default constructor.
Definition: CTicTac.cpp:60
AuxWindowsTicTac() noexcept
Definition: CTicTac.cpp:41
double dbl_period
Definition: CTicTac.cpp:36
static AuxWindowsTicTac & GetInstance() noexcept
Definition: CTicTac.cpp:30
LARGE_INTEGER m_freq
Definition: CTicTac.cpp:39
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:76
#define LARGE_INTEGER_NUMS
Definition: CTicTac.cpp:55
unsigned long largeInts[4]



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 5711e29ae Wed May 27 14:29:47 2020 +0200 at miƩ may 27 14:30:10 CEST 2020