timer.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2015-2019 CNRS INRIA
3 //
4 
5 #ifndef __pinocchio_utils_timer_hpp__
6 #define __pinocchio_utils_timer_hpp__
7 
8 #ifdef WIN32
9  #include <Windows.h>
10  #include <stdint.h> // portable: uint64_t MSVC: __int64
11 
12 int gettimeofday(struct timeval * tp, struct timezone * tzp)
13 {
14  // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
15  // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
16  // until 00:00:00 January 1, 1970
17  static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
18 
19  SYSTEMTIME system_time;
20  FILETIME file_time;
21  uint64_t time;
22 
23  GetSystemTime(&system_time);
24  SystemTimeToFileTime(&system_time, &file_time);
25  time = ((uint64_t)file_time.dwLowDateTime);
26  time += ((uint64_t)file_time.dwHighDateTime) << 32;
27 
28  tp->tv_sec = (long)((time - EPOCH) / 10000000L);
29  tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
30  return 0;
31 }
32 #else
33  #include <sys/time.h>
34 #endif
35 #include <iostream>
36 #include <stack>
37 
38 #define SMOOTH(s) for (size_t _smooth = 0; _smooth < s; ++_smooth)
39 
40 /* Return the time spent in secs. */
41 inline double operator-(const struct timeval & t1, const struct timeval & t0)
42 {
43  /* TODO: double check the double conversion from long (on 64x). */
44  return double(t1.tv_sec - t0.tv_sec) + 1e-6 * double(t1.tv_usec - t0.tv_usec);
45 }
46 
48 {
49  enum Unit
50  {
51  S = 1,
52  MS = 1000,
53  US = 1000000,
54  NS = 1000000000
55  };
57 
58  static std::string unitName(Unit u)
59  {
60  switch (u)
61  {
62  case S:
63  return "s";
64  case MS:
65  return "ms";
66  case US:
67  return "us";
68  case NS:
69  return "ns";
70  }
71  return "";
72  }
73 
74  std::stack<struct timeval> stack;
75  mutable struct timeval t0;
76 
78  : DEFAULT_UNIT(def)
79  {
80  }
81 
82  inline void tic()
83  {
84  stack.push(t0);
85  gettimeofday(&(stack.top()), NULL);
86  }
87 
88  inline double toc()
89  {
90  return toc(DEFAULT_UNIT);
91  };
92 
93  inline double toc(const Unit factor)
94  {
95  gettimeofday(&t0, NULL);
96  double dt = (t0 - stack.top()) * (double)factor;
97  stack.pop();
98  return dt;
99  }
100 
101  inline void toc(std::ostream & os, double SMOOTH = 1)
102  {
103  os << toc(DEFAULT_UNIT) / SMOOTH << " " << unitName(DEFAULT_UNIT) << std::endl;
104  }
105 };
106 
107 #endif // ifndef __pinocchio_utils_timer_hpp__
PinocchioTicToc::S
@ S
Definition: timer.hpp:51
PinocchioTicToc::toc
double toc()
Definition: timer.hpp:88
operator-
double operator-(const struct timeval &t1, const struct timeval &t0)
Definition: timer.hpp:41
PinocchioTicToc::t0
struct timeval t0
Definition: timer.hpp:75
factor
Definition: factor.py:1
PinocchioTicToc::PinocchioTicToc
PinocchioTicToc(Unit def=MS)
Definition: timer.hpp:77
L
L
uint64_t
uint64_t
PinocchioTicToc::unitName
static std::string unitName(Unit u)
Definition: timer.hpp:58
def
void def(const char *name, Func func)
PinocchioTicToc::DEFAULT_UNIT
Unit DEFAULT_UNIT
Definition: timer.hpp:56
SMOOTH
#define SMOOTH(s)
Definition: timer.hpp:38
PinocchioTicToc::toc
double toc(const Unit factor)
Definition: timer.hpp:93
continuous.u
u
Definition: continuous.py:181
PinocchioTicToc::Unit
Unit
Definition: timer.hpp:49
PinocchioTicToc::US
@ US
Definition: timer.hpp:53
PinocchioTicToc::NS
@ NS
Definition: timer.hpp:54
PinocchioTicToc::MS
@ MS
Definition: timer.hpp:52
PinocchioTicToc
Definition: timer.hpp:47
cartpole.dt
float dt
Definition: cartpole.py:145
PinocchioTicToc::toc
void toc(std::ostream &os, double SMOOTH=1)
Definition: timer.hpp:101
PinocchioTicToc::stack
std::stack< struct timeval > stack
Definition: timer.hpp:74
PinocchioTicToc::tic
void tic()
Definition: timer.hpp:82


pinocchio
Author(s):
autogenerated on Thu Dec 19 2024 03:41:33