timer.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 #ifndef __SOT_TIMER_HH
11 #define __SOT_TIMER_HH
12 
13 /* --------------------------------------------------------------------- */
14 /* --- INCLUDE --------------------------------------------------------- */
15 /* --------------------------------------------------------------------- */
16 
17 /* Classes standards. */
18 #include <list> /* Classe std::list */
19 #ifndef WIN32
20 #include <sys/time.h>
21 #else /*WIN32*/
22 // When including Winsock2.h, the MAL must be included first
23 #include <Winsock2.h>
25 
27 #endif /*WIN32*/
28 
29 /* SOT */
31 #include <dynamic-graph/entity.h>
32 
33 #include <sot/core/debug.hh>
34 
35 /* --------------------------------------------------------------------- */
36 /* --- API ------------------------------------------------------------- */
37 /* --------------------------------------------------------------------- */
38 
39 #if defined(WIN32)
40 #if defined(timer_EXPORTS)
41 #define Timer_EXPORT __declspec(dllexport)
42 #else
43 #define Timer_EXPORT __declspec(dllimport)
44 #endif
45 #else
46 #define Timer_EXPORT
47 #endif
48 
49 /* --------------------------------------------------------------------- */
50 /* --- CLASS ----------------------------------------------------------- */
51 /* --------------------------------------------------------------------- */
52 
53 template <class T>
55  public:
56  static const std::string CLASS_NAME;
57  virtual const std::string &getClassName(void) const { return CLASS_NAME; }
58 
59  protected:
60  struct timeval t0, t1;
61  clock_t c0, c1;
62  double dt;
63 
64  public:
65  /* --- CONSTRUCTION --- */
66  Timer(const std::string &name);
67 
68  public: /* --- DISPLAY --- */
69  virtual void display(std::ostream &os) const;
70  Timer_EXPORT friend std::ostream &operator<<(std::ostream &os,
71  const Timer<T> &timer) {
72  timer.display(os);
73  return os;
74  }
75 
76  public: /* --- SIGNALS --- */
81 
82  protected: /* --- SIGNAL FUNCTIONS --- */
84  sigSIN = &sig;
85  dt = 0.;
86  }
87 
88  template <bool UseClock>
89  T &compute(T &t, const int &time) {
90  sotDEBUGIN(15);
91  if (UseClock) {
92  c0 = clock();
93  sotDEBUG(15) << "t0: " << c0 << std::endl;
94  } else {
95  gettimeofday(&t0, NULL);
96  sotDEBUG(15) << "t0: " << t0.tv_sec << " - " << t0.tv_usec << std::endl;
97  }
98 
99  t = sigSIN(time);
100 
101  if (UseClock) {
102  c1 = clock();
103  sotDEBUG(15) << "t1: " << c0 << std::endl;
104  dt = ((double)(c1 - c0) * 1000) / CLOCKS_PER_SEC;
105  } else {
106  gettimeofday(&t1, NULL);
107  dt = ((static_cast<double>(t1.tv_sec) - static_cast<double>(t0.tv_sec)) *
108  1000. +
109  (static_cast<double>(t1.tv_usec) - static_cast<double>(t0.tv_usec) +
110  0.) /
111  1000.);
112  sotDEBUG(15) << "t1: " << t1.tv_sec << " - " << t1.tv_usec << std::endl;
113  }
114 
115  timerSOUT = dt;
116  timerSOUT.setTime(time);
117 
118  sotDEBUGOUT(15);
119  return t;
120  }
121 
122  double &getDt(double &res, const int & /*time*/) {
123  res = dt;
124  return res;
125  }
126 };
127 
128 void cmdChrono(const std::string &cmd, std::istringstream &args,
129  std::ostream &os);
130 
131 /* --------------------------------------------------------------------- */
132 /* --------------------------------------------------------------------- */
133 /* --------------------------------------------------------------------- */
134 
135 /* --- CONSTRUCTION ---------------------------------------------------- */
136 template <class T>
137 Timer<T>::Timer(const std::string &name)
138  : Entity(name),
139  dt(0.),
140  sigSIN(NULL, "Timer(" + name + ")::input(T)::sin"),
141  sigSOUT(boost::bind(&Timer::compute<false>, this, _1, _2), sigSIN,
142  "Timer(" + name + ")::output(T)::sout"),
143  sigClockSOUT(boost::bind(&Timer::compute<true>, this, _1, _2), sigSIN,
144  "Timer(" + name + ")::output(T)::clockSout"),
145  timerSOUT("Timer(" + name + ")::output(double)::timer") {
146  sotDEBUGIN(15);
147  timerSOUT.setFunction(boost::bind(&Timer::getDt, this, _1, _2));
148 
150  sotDEBUGOUT(15);
151 }
152 
153 /* --- DISPLAY --------------------------------------------------------- */
154 template <class T>
155 void Timer<T>::display(std::ostream &os) const {
156  os << "Timer <" << sigSIN << "> : " << dt << "ms." << std::endl;
157 }
158 
159 #endif /* #ifndef __SOT_SOT_HH */
dynamicgraph::SignalTimeDependent< T, int > sigClockSOUT
Definition: timer.hh:79
dynamicgraph::SignalPtr< T, int > sigSIN
Definition: timer.hh:77
int T
void signalRegistration(const SignalArray< int > &signals)
double & getDt(double &res, const int &)
Definition: timer.hh:122
#define sotDEBUGOUT(level)
Definition: debug.hh:212
Signal< dynamicgraph::Matrix, int > sig("matrix")
static const std::string CLASS_NAME
Definition: timer.hh:56
#define sotDEBUGIN(level)
Definition: debug.hh:211
Timer_EXPORT friend std::ostream & operator<<(std::ostream &os, const Timer< T > &timer)
Definition: timer.hh:70
Timer(const std::string &name)
Definition: timer.hh:137
double dt
Definition: timer.hh:62
virtual void display(std::ostream &os) const
clock_t c1
Definition: timer.hh:61
#define sotDEBUG(level)
Definition: debug.hh:165
args
virtual void display(std::ostream &os) const
Definition: timer.hh:155
Transform3f t
dynamicgraph::SignalTimeDependent< T, int > sigSOUT
Definition: timer.hh:78
dynamicgraph::Signal< double, int > timerSOUT
Definition: timer.hh:80
virtual const std::string & getClassName(void) const
Definition: timer.hh:57
#define Timer_EXPORT
Definition: timer.hh:46
Definition: timer.hh:54
void plug(dynamicgraph::Signal< T, int > &sig)
Definition: timer.hh:83
virtual void setTime(const Time &t)
T & compute(T &t, const int &time)
Definition: timer.hh:89
virtual void setFunction(boost::function2< T &, T &, Time > t, Mutex *mutexref=NULL)
void cmdChrono(const std::string &cmd, std::istringstream &args, std::ostream &os)
Definition: timer.cpp:45


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Wed Jun 21 2023 02:51:26