timer.h
Go to the documentation of this file.
1 #include <sys/time.h>
2 #include <iostream>
3 #include <stack>
4 
5 #define SMOOTH(s) for(int _smooth=0;_smooth<s;++_smooth)
6 
7 /* Return the time spent in secs. */
8 inline double operator- ( const struct timeval & t1,const struct timeval & t0)
9 {
10  return (t1.tv_sec - t0.tv_sec)+1e-6*(t1.tv_usec - t0.tv_usec);
11 }
12 
14 {
15  enum Unit { S = 1, MS = 1000, US = 1000000 };
17  static std::string unitName(Unit u)
18  { switch(u) { case S: return "s"; case MS: return "ms"; case US: return "us"; } return ""; }
19 
20  std::stack<struct timeval> stack;
21  mutable struct timeval t0;
22 
23 StackTicToc( Unit def = MS ) : DEFAULT_UNIT(def) {}
24 
25  inline void tic() {
26  stack.push(t0);
27  gettimeofday(&(stack.top()),NULL);
28  }
29 
30  inline double toc(const Unit factor)
31  {
32  gettimeofday(&t0,NULL);
33  double dt = (t0-stack.top())*factor;
34  stack.pop();
35  return dt;
36  }
37  inline void toc( std::ostream& os, double SMOOTH=1 )
38  {
39  os << toc(DEFAULT_UNIT)/SMOOTH << " " << unitName(DEFAULT_UNIT) << std::endl;
40  }
41 };
42 
#define SMOOTH(s)
Definition: timer.h:5
static std::string unitName(Unit u)
Definition: timer.h:17
double toc(const Unit factor)
Definition: timer.h:30
void toc(std::ostream &os, double SMOOTH=1)
Definition: timer.h:37
Unit DEFAULT_UNIT
Definition: timer.h:16
std::stack< struct timeval > stack
Definition: timer.h:20
struct timeval t0
Definition: timer.h:21
void tic()
Definition: timer.h:25
StackTicToc(Unit def=MS)
Definition: timer.h:23
double operator-(const struct timeval &t1, const struct timeval &t0)
Definition: timer.h:8


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Apr 17 2021 02:37:59