Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

XFuTimingCounter.h

Go to the documentation of this file.
00001 /*! \file
00002  * X-Forge Util <br>
00003  * Copyright 2000-2003 Fathammer Ltd
00004  *
00005  * \brief XFuConfiguration.h is the header file for the XFuConfiguration class.
00006  *
00007  * $Id: XFuTimingCounter.h,v 1.4 2003/08/12 13:33:52 lars Exp $
00008  * $Date: 2003/08/12 13:33:52 $
00009  * $Revision: 1.4 $
00010  */
00011 
00012 #ifndef XFUTIMINGCOUNTER_H_INCLUDED
00013 #define XFUTIMINGCOUNTER_H_INCLUDED
00014 
00015 #define XFU_TIMINGCOUNTER_STARTED 1
00016 #define XFU_TIMINGCOUNTER_RUNNING 2
00017 
00018 //! Timing counter that takes average of a definable amount of samples.
00019 /*!
00020  *  This counter can be used for evaluating time spend within functions even in the
00021  *  case the time is less than the resolution of the clock. The timer takes sliding
00022  *  average of large amount of samples, and thus gives results that are statistically
00023  *  more accurate than the clock itself.
00024  */
00025 class XFuTimingCounter
00026 {
00027 public:
00028     //! Constructs the timing counter with aSampleCount amount of samples
00029     static XFuTimingCounter *create(INT32 aSampleCount)
00030     {
00031         if (aSampleCount <= 0)
00032             return NULL;
00033 
00034         XFuTimingCounter *counter = new XFuTimingCounter();
00035 
00036         if (counter)
00037         {
00038             counter->mSamples = new INT32[aSampleCount];
00039             if (counter->mSamples == NULL)
00040             {
00041                 delete counter;
00042                 return NULL;
00043             }
00044 
00045             counter->mMaxSampleCount = aSampleCount;
00046         }
00047 
00048         return counter;
00049     }
00050 
00051     //! Starts counting of one timing iteration
00052     virtual void start()
00053     {
00054         mTickDiff = 0;
00055         mState = XFU_TIMINGCOUNTER_STARTED;
00056         resume();
00057     }
00058 
00059     //! Stops counting of one timing iteration
00060     virtual void stop()
00061     {
00062         if (mState & XFU_TIMINGCOUNTER_STARTED)
00063         {
00064             pause();
00065             INT32 lastTick = mSamples[mCurrentSample];
00066             mSampleCount++;
00067             if (mSampleCount > mMaxSampleCount)
00068             {
00069                 mSampleCount = mMaxSampleCount;
00070                 mFirstSample = (mFirstSample + 1) % mSampleCount;
00071             }
00072             mCurrentSample = (mCurrentSample + 1) % mSampleCount;
00073             mSamples[mCurrentSample] = lastTick + mTickDiff;
00074             mState = 0;
00075         }
00076     }
00077 
00078 
00079     //! Pauses the counting
00080     virtual void pause()
00081     {
00082         if ((mState & (XFU_TIMINGCOUNTER_STARTED | XFU_TIMINGCOUNTER_RUNNING)) == 
00083             (XFU_TIMINGCOUNTER_STARTED | XFU_TIMINGCOUNTER_RUNNING))
00084         {
00085             INT32 tick = XFcCore::getTick();
00086             mTickDiff = mTickDiff + (tick - mCurrentTick);
00087             mState &= ~XFU_TIMINGCOUNTER_RUNNING;
00088         }
00089     }
00090 
00091     //! Resumes the counting
00092     virtual void resume()
00093     {
00094         if ((mState & (XFU_TIMINGCOUNTER_STARTED | XFU_TIMINGCOUNTER_RUNNING)) == 
00095             XFU_TIMINGCOUNTER_STARTED)
00096         {
00097             mCurrentTick = XFcCore::getTick();
00098             mState |= XFU_TIMINGCOUNTER_RUNNING;
00099         }
00100     }
00101 
00102     //! Resets the counter
00103     virtual void reset()
00104     {
00105         mSampleCount = 0;
00106         mCurrentSample = 0;
00107         mFirstSample = 0;
00108         mState = 0;
00109     }
00110 
00111     //! Returns the average time spent between start() and stop() calls
00112     virtual REAL getTime()
00113     {
00114         if (mSampleCount > 1)
00115             return (REAL)(mSamples[mCurrentSample] - mSamples[mFirstSample]) / (mSampleCount - 1);
00116         else
00117             return REALi(0);
00118     }
00119 
00120     //! Virtual destructor
00121     virtual ~XFuTimingCounter()
00122     {
00123         delete[] mSamples;
00124     }
00125 
00126 protected:
00127     XFuTimingCounter()
00128     {
00129         mSamples = NULL;
00130         mSampleCount = 0;
00131         mMaxSampleCount = 0;
00132         mCurrentSample = 0;
00133         mFirstSample = 0;
00134     }
00135 
00136     INT32 *mSamples;
00137     INT32 mSampleCount;
00138     INT32 mMaxSampleCount;
00139     INT32 mFirstSample;
00140     INT32 mCurrentSample;
00141     INT32 mCurrentTick;
00142     INT32 mTickDiff;
00143     INT32 mState;
00144 };
00145 
00146 #endif //XFUTIMINGCOUNTER_H_INCLUDED
00147 

   
X-Forge Documentation
Confidential
Copyright © 2002-2003 Fathammer
   
Documentation generated
with doxygen
by Dimitri van Heesch