iCub-main
testStats.h
Go to the documentation of this file.
1 #include <stdio.h>
2 
3 #include <yarp/os/Thread.h>
4 #include <yarp/os/Time.h>
5 #include <yarp/os/Network.h>
6 
7 class Stats
8 {
9 private:
10 
11  double totalUsed; //total time taken iterations
12  unsigned int count; //number of iterations from last reset
13  unsigned int estPIt; //number of useful iterations for period estimation
14  double totalT; //time bw run, accumulated
15  double sumTSq; //cumulative sum sq of estimated period dT
16  double sumUsedSq; //cumulative sum sq of estimated thread tun
17  double previousRun; //time when last iteration started
18  double currentRun; //time when this iteration started
19  // AC
20  double elapsed; //time between start and end
21  bool scheduleReset;
22 
23  void _resetStat() {
24  totalUsed=0;
25  count=0;
26  estPIt=0;
27  totalT=0;
28  sumUsedSq=0;
29  sumTSq=0;
30  }
31 
32 public:
33 
34  Stats(){
35  _resetStat();
36  }
37 
38  void resetStat() {
39  _resetStat();
40  }
41 
42  double getEstPeriod() {
43  double ret;
44 
45  if (estPIt==0)
46  ret=0;
47  else
48  ret=totalT/estPIt;
49 
50  return ret;
51  }
52 
53  void getEstPeriod(double &av, double &std) {
54 
55  if (estPIt==0) {
56  av=0;
57  std=0;
58  } else {
59  av=totalT/estPIt;
60  if (estPIt>1) {
61  std=sqrt(((1.0/(estPIt-1))*(sumTSq-estPIt*av*av)));
62  } else {
63  std=0;
64  }
65  }
66 
67  }
68 
69  unsigned int getIterations() {
70  unsigned int ret=count;
71 
72  return ret;
73  }
74 
75  double getEstUsed() {
76  double ret;
77 
78  if (count<1)
79  ret=0.0;
80  else
81  ret=totalUsed/count;
82 
83  return ret;
84  }
85 
86  void getEstUsed(double &av, double &std) {
87 
88  if (count<1) {
89  av=0;
90  std=0;
91  } else {
92  av=totalUsed/count;
93  if (count>1) {
94  std=sqrt((1.0/(count-1))*(sumUsedSq-count*av*av));
95  } else {
96  std=0;
97  }
98  }
99 
100  }
101 
102 
103  void tickStart() {
104  currentRun=yarp::os::Time::now();
105 
106  if (count>0) {
107  //double saved=adaptedPeriod;
108  double dT=(currentRun-previousRun)*1000;
109  sumTSq+=dT*dT;
110  totalT+=dT;
111  estPIt++;
112  }
113 
114  previousRun=currentRun;
115  count++;
116  }
117 
118  void tickEnd()
119  {
120  elapsed=(yarp::os::Time::now()-currentRun) * 1000;
121 
122  //save last
123  totalUsed+=elapsed;
124  sumUsedSq+=elapsed*elapsed;
125  }
126 
127  //AC
128  double getElapsed()
129  {
130  return elapsed;
131  }
132 };
Definition: testStats.h:8
unsigned int getIterations()
Definition: testStats.h:69
void resetStat()
Definition: testStats.h:38
double getEstPeriod()
Definition: testStats.h:42
double getElapsed()
Definition: testStats.h:128
void tickStart()
Definition: testStats.h:103
void tickEnd()
Definition: testStats.h:118
double getEstUsed()
Definition: testStats.h:75
void getEstUsed(double &av, double &std)
Definition: testStats.h:86
void getEstPeriod(double &av, double &std)
Definition: testStats.h:53
Stats()
Definition: testStats.h:34