# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

stop_watch.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "stdmisc.h"
00027 
00028 #include "nel/misc/stop_watch.h"
00029 #include <numeric>
00030 
00031 using namespace std;
00032 
00033 namespace NLMISC {
00034 
00035 
00036 /*
00037  * Constructor
00038  */
00039 CStopWatch::CStopWatch( uint queueLength ) :
00040         _BeginTime( 0 ),
00041         _ElapsedTicks( 0 ),
00042         _SumTicks( 0 ),
00043         _MeasurementNumber( 0 ),
00044         _Queue(),
00045         _QLength( queueLength )
00046 {}
00047 
00048 
00049 /*
00050  * Begin measurement
00051  */
00052 void    CStopWatch::start()
00053 {
00054         _BeginTime = CTime::getPerformanceTime();
00055         _ElapsedTicks = 0;
00056 }
00057 
00058 
00059 /*
00060  * Pause
00061  */
00062 void    CStopWatch::pause()
00063 {
00064         _ElapsedTicks += (TTickDuration)(CTime::getPerformanceTime() - _BeginTime);
00065 }
00066 
00067 
00068 /*
00069  * Resume
00070  */
00071 void    CStopWatch::resume()
00072 {
00073         _BeginTime = CTime::getPerformanceTime();
00074 }
00075 
00076 
00077 /*
00078  * Add time (in TTicks unit) to the current measurement
00079  */
00080 void    CStopWatch::addTime( TTickDuration t )
00081 {
00082         _ElapsedTicks += t;
00083 }
00084 
00085 
00086 /*
00087  * End measurement
00088  */
00089 void    CStopWatch::stop()
00090 {
00091         _ElapsedTicks += (TTickDuration)(CTime::getPerformanceTime() - _BeginTime);
00092 
00093         // Setup average
00094         _SumTicks += _ElapsedTicks;
00095         ++_MeasurementNumber;
00096 
00097         // Setup partial average
00098         if ( _QLength != 0 )
00099         {
00100                 _Queue.push_back( _ElapsedTicks );
00101                 if ( _Queue.size() > _QLength )
00102                 {
00103                         _Queue.pop_front();
00104                 }
00105         }
00106 }
00107 
00108 
00109 /*
00110  * Add an external duration (in TTicks unit) to the average queue
00111  */
00112 void    CStopWatch::addMeasurement( TTickDuration t )
00113 {
00114         // Setup average
00115         _SumTicks += t;
00116         ++_MeasurementNumber;
00117 
00118         // Setup partial average
00119         if ( _QLength != 0 )
00120         {
00121                 _Queue.push_back( t );
00122                 if ( _Queue.size() > _QLength )
00123                 {
00124                         _Queue.pop_front();
00125                 }
00126         }
00127 
00128 }
00129 
00130 
00131 /*
00132  * Elapsed time in millisecond (call it after stop())
00133  */
00134 TMsDuration     CStopWatch::getDuration() const
00135 {
00136         return (TMsDuration)(CTime::ticksToSecond( _ElapsedTicks ) * 1000.0);
00137 }
00138 
00139 
00140 /*
00141  * Average of the queueLength last durations (using the queueLength argument specified in the constructor)
00142  */
00143 TMsDuration     CStopWatch::getPartialAverage() const
00144 {
00145         if (_Queue.size() == 0)
00146                 return (TMsDuration)0;
00147         else
00148                 return (TMsDuration)(CTime::ticksToSecond( accumulate( _Queue.begin(), _Queue.end(), 0 ) / _Queue.size() ) * 1000.0);
00149 }
00150 
00151 
00152 /*
00153  * Average of the duration
00154  */
00155 TMsDuration     CStopWatch::getAverageDuration() const
00156 {
00157         return (TMsDuration)(CTime::ticksToSecond( _SumTicks / _MeasurementNumber ) * 1000.0);
00158 }
00159 
00160 
00161 
00162 } // NLMISC