1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "nel/misc/stop_watch.h"
33 CStopWatch::CStopWatch( uint queueLength
) :
37 _MeasurementNumber( 0 ),
39 _QLength( queueLength
)
46 void CStopWatch::start()
48 _BeginTime
= CTime::getPerformanceTime();
56 void CStopWatch::pause()
58 _ElapsedTicks
+= (TTickDuration
)(CTime::getPerformanceTime() - _BeginTime
);
65 void CStopWatch::resume()
67 _BeginTime
= CTime::getPerformanceTime();
72 * Add time (in TTicks unit) to the current measurement
74 void CStopWatch::addTime( TTickDuration t
)
83 void CStopWatch::stop()
85 _ElapsedTicks
+= (TTickDuration
)(CTime::getPerformanceTime() - _BeginTime
);
88 _SumTicks
+= _ElapsedTicks
;
91 // Setup partial average
94 _Queue
.push_back( _ElapsedTicks
);
95 if ( _Queue
.size() > _QLength
)
104 * Add an external duration (in TTicks unit) to the average queue
106 void CStopWatch::addMeasurement( TTickDuration t
)
110 ++_MeasurementNumber
;
112 // Setup partial average
115 _Queue
.push_back( t
);
116 if ( _Queue
.size() > _QLength
)
126 * Elapsed time in millisecond (call it after stop())
128 TMsDuration
CStopWatch::getDuration() const
130 return (TMsDuration
)(CTime::ticksToSecond( _ElapsedTicks
) * 1000.0);
135 * Average of the queueLength last durations (using the queueLength argument specified in the constructor)
137 TMsDuration
CStopWatch::getPartialAverage() const
140 return (TMsDuration
)0;
142 return (TMsDuration
)(CTime::ticksToSecond( accumulate( _Queue
.begin(), _Queue
.end(), 0 ) / _Queue
.size() ) * 1000.0);
147 * Average of the duration
149 TMsDuration
CStopWatch::getAverageDuration() const
151 return (TMsDuration
)(CTime::ticksToSecond( _SumTicks
/ _MeasurementNumber
) * 1000.0);