Skip to content

Commit fabb6e1

Browse files
committed
Add ability to use clock instead of gettimeofday in entity Timer
1 parent 6b975c1 commit fabb6e1

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

include/sot/core/timer.hh

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Timer_EXPORT Timer
7171
protected:
7272

7373
struct timeval t0,t1;
74+
clock_t c0, c1;
7475
double dt;
7576

7677
public:
@@ -87,6 +88,7 @@ class Timer_EXPORT Timer
8788

8889
dg::SignalPtr<T,int> sigSIN;
8990
dg::SignalTimeDependent<T,int> sigSOUT;
91+
dg::SignalTimeDependent<T,int> sigClockSOUT;
9092
dg::Signal<double,int> timerSOUT;
9193

9294

@@ -96,18 +98,30 @@ class Timer_EXPORT Timer
9698
sigSIN = &sig; dt=0.;
9799
}
98100

101+
template <bool UseClock>
99102
T& compute( T& t,const int& time )
100103
{
101104
sotDEBUGIN(15);
102-
gettimeofday(&t0,NULL);
103-
sotDEBUG(15) << "t0: "<< t0.tv_sec << " - " << t0.tv_usec << std::endl;
105+
if (UseClock) {
106+
c0 = clock();
107+
sotDEBUG(15) << "t0: "<< c0 << std::endl;
108+
} else {
109+
gettimeofday(&t0,NULL);
110+
sotDEBUG(15) << "t0: "<< t0.tv_sec << " - " << t0.tv_usec << std::endl;
111+
}
104112

105113
t = sigSIN( time );
106114

107-
gettimeofday(&t1,NULL);
108-
dt = ( (t1.tv_sec-t0.tv_sec) * 1000.
109-
+ (t1.tv_usec-t0.tv_usec+0.) / 1000. );
110-
sotDEBUG(15) << "t1: "<< t1.tv_sec << " - " << t1.tv_usec << std::endl;
115+
if (UseClock) {
116+
c1 = clock();
117+
sotDEBUG(15) << "t1: "<< c0 << std::endl;
118+
dt = ((double)(c1 - c0) * 1000 ) / CLOCKS_PER_SEC;
119+
} else {
120+
gettimeofday(&t1,NULL);
121+
dt = ( (t1.tv_sec-t0.tv_sec) * 1000.
122+
+ (t1.tv_usec-t0.tv_usec+0.) / 1000. );
123+
sotDEBUG(15) << "t1: "<< t1.tv_sec << " - " << t1.tv_usec << std::endl;
124+
}
111125

112126
timerSOUT = dt;
113127
timerSOUT.setTime (time);
@@ -146,18 +160,20 @@ template< class T >
146160
Timer<T>::
147161
Timer( const std::string& name )
148162
:Entity(name)
149-
,t0(),t1()
150163
,dt(0.)
151164
,sigSIN( NULL,"Timer("+name+")::input(T)::sin" )
152-
,sigSOUT( boost::bind(&Timer::compute,this,_1,_2),
165+
,sigSOUT( boost::bind(&Timer::compute<false>,this,_1,_2),
166+
sigSIN,
167+
"Timer("+name+")::output(T)::sout" )
168+
,sigClockSOUT( boost::bind(&Timer::compute<true>,this,_1,_2),
153169
sigSIN,
154170
"Timer("+name+")::output(T)::sout" )
155171
,timerSOUT( "Timer("+name+")::output(double)::timer" )
156172
{
157173
sotDEBUGIN(15);
158174
timerSOUT.setFunction( boost::bind(&Timer::getDt,this,_1,_2) );
159175

160-
signalRegistration( sigSIN<<sigSOUT<<timerSOUT );
176+
signalRegistration( sigSIN<<sigSOUT<<sigClockSOUT<<timerSOUT );
161177
sotDEBUGOUT(15);
162178
}
163179

0 commit comments

Comments
 (0)