Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / src / misc / stop_watch.cpp
blob8da60d8eccd906c627ef10ca25857a27d40f283b
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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/>.
17 #include "stdmisc.h"
19 #include "nel/misc/stop_watch.h"
21 using namespace std;
23 #ifdef DEBUG_NEW
24 #define new DEBUG_NEW
25 #endif
27 namespace NLMISC {
31 * Constructor
33 CStopWatch::CStopWatch( uint queueLength ) :
34 _BeginTime( 0 ),
35 _ElapsedTicks( 0 ),
36 _SumTicks( 0 ),
37 _MeasurementNumber( 0 ),
38 _Queue(),
39 _QLength( queueLength )
44 * Begin measurement
46 void CStopWatch::start()
48 _BeginTime = CTime::getPerformanceTime();
49 _ElapsedTicks = 0;
54 * Pause
56 void CStopWatch::pause()
58 _ElapsedTicks += (TTickDuration)(CTime::getPerformanceTime() - _BeginTime);
63 * Resume
65 void CStopWatch::resume()
67 _BeginTime = CTime::getPerformanceTime();
72 * Add time (in TTicks unit) to the current measurement
74 void CStopWatch::addTime( TTickDuration t )
76 _ElapsedTicks += t;
81 * End measurement
83 void CStopWatch::stop()
85 _ElapsedTicks += (TTickDuration)(CTime::getPerformanceTime() - _BeginTime);
87 // Setup average
88 _SumTicks += _ElapsedTicks;
89 ++_MeasurementNumber;
91 // Setup partial average
92 if ( _QLength != 0 )
94 _Queue.push_back( _ElapsedTicks );
95 if ( _Queue.size() > _QLength )
97 _Queue.pop_front();
104 * Add an external duration (in TTicks unit) to the average queue
106 void CStopWatch::addMeasurement( TTickDuration t )
108 // Setup average
109 _SumTicks += t;
110 ++_MeasurementNumber;
112 // Setup partial average
113 if ( _QLength != 0 )
115 _Queue.push_back( t );
116 if ( _Queue.size() > _QLength )
118 _Queue.pop_front();
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
139 if (_Queue.empty())
140 return (TMsDuration)0;
141 else
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);
156 } // NLMISC