MRPT  2.0.0
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 #endif
17 
18 #include <mrpt/core/exceptions.h>
19 #include <mrpt/system/CTicTac.h>
20 #include <cassert>
21 #include <cstring>
22 
23 #include <type_traits>
24 
25 // For Windows: get the common code out of CTicTac so it's only run once!
26 #ifdef _WIN32
28 {
29  static AuxWindowsTicTac& GetInstance() noexcept
30  {
31  static AuxWindowsTicTac obj;
32  return obj;
33  }
34 
35  double dbl_period;
36 
37  private:
38  LARGE_INTEGER m_freq;
39 
40  AuxWindowsTicTac() noexcept : dbl_period(.0)
41  {
42  QueryPerformanceFrequency(&m_freq);
43  assert(m_freq.QuadPart != 0);
44  dbl_period = 1.0 / static_cast<double>(m_freq.QuadPart);
45  }
46 };
47 
48 #endif
49 
50 using namespace mrpt::system;
51 
52 // Macros for easy access to memory with the correct types:
53 #ifdef _WIN32
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 CTicTac::CTicTac() noexcept
60 {
61  ::memset(largeInts, 0, sizeof(largeInts));
62 
63 #ifdef _WIN32
64  static_assert(
65  sizeof(largeInts) >= 2 * sizeof(LARGE_INTEGER),
66  "sizeof(LARGE_INTEGER) failed!");
67 #else
68  static_assert(
69  sizeof(largeInts) >= 2 * sizeof(struct timeval),
70  "sizeof(struct timeval) failed!");
71 #endif
72  Tic();
73 }
74 
75 void CTicTac::Tic() noexcept
76 {
77 #ifdef _WIN32
78  LARGE_INTEGER* l = LARGE_INTEGER_NUMS;
79  QueryPerformanceCounter(&l[0]);
80 #else
81  auto* ts = TIMEVAL_NUMS;
82  gettimeofday(&ts[0], nullptr);
83 #endif
84 }
85 
86 double CTicTac::Tac() noexcept
87 {
88 #ifdef _WIN32
89  LARGE_INTEGER* l = LARGE_INTEGER_NUMS;
90  QueryPerformanceCounter(&l[1]);
91  return (l[1].QuadPart - l[0].QuadPart) *
93 #else
94  auto* ts = TIMEVAL_NUMS;
95  gettimeofday(&ts[1], nullptr);
96  return static_cast<double>(ts[1].tv_sec - ts[0].tv_sec) +
97  1e-6 * static_cast<double>(ts[1].tv_usec - ts[0].tv_usec);
98 #endif
99 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
CTicTac() noexcept
Default constructor.
Definition: CTicTac.cpp:59
AuxWindowsTicTac() noexcept
Definition: CTicTac.cpp:40
double dbl_period
Definition: CTicTac.cpp:35
static AuxWindowsTicTac & GetInstance() noexcept
Definition: CTicTac.cpp:29
LARGE_INTEGER m_freq
Definition: CTicTac.cpp:38
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:75
#define LARGE_INTEGER_NUMS
Definition: CTicTac.cpp:54
unsigned long largeInts[4]



Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020