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 { S = 1, MS = 1000, US = 1000000, NS = 1000000000 };
51 
52  static std::string unitName(Unit u)
53  {
54  switch(u) { case S: return "s"; case MS: return "ms"; case US: return "us"; case NS: return "ns"; }
55  return "";
56  }
57 
58  std::stack<struct timeval> stack;
59  mutable struct timeval t0;
60 
61  PinocchioTicToc( Unit def = MS ) : DEFAULT_UNIT(def) {}
62 
63  inline void tic() {
64  stack.push(t0);
65  gettimeofday(&(stack.top()),NULL);
66  }
67 
68  inline double toc() { return toc(DEFAULT_UNIT); };
69 
70  inline double toc(const Unit factor)
71  {
72  gettimeofday(&t0,NULL);
73  double dt = (t0-stack.top())*(double)factor;
74  stack.pop();
75  return dt;
76  }
77 
78  inline void toc(std::ostream & os, double SMOOTH=1)
79  {
80  os << toc(DEFAULT_UNIT)/SMOOTH << " " << unitName(DEFAULT_UNIT) << std::endl;
81  }
82 };
83 
84 #endif // ifndef __pinocchio_utils_timer_hpp__
Definition: factor.py:1
double toc()
Definition: timer.hpp:68
Unit DEFAULT_UNIT
Definition: timer.hpp:50
static std::string unitName(Unit u)
Definition: timer.hpp:52
double operator-(const struct timeval &t1, const struct timeval &t0)
Definition: timer.hpp:41
void def(const char *name, Func func)
void tic()
Definition: timer.hpp:63
struct timeval t0
Definition: timer.hpp:59
PinocchioTicToc(Unit def=MS)
Definition: timer.hpp:61
#define SMOOTH(s)
Definition: timer.hpp:38
void toc(std::ostream &os, double SMOOTH=1)
Definition: timer.hpp:78
std::stack< struct timeval > stack
Definition: timer.hpp:58
double toc(const Unit factor)
Definition: timer.hpp:70
L


pinocchio
Author(s):
autogenerated on Fri Jun 23 2023 02:38:33