00001
00002
00003 #include <time.h>
00004 #include <sys/time.h>
00005 #include <math.h>
00006 #include "gtime.h"
00007
00008
00009
00010 gTimer::gTimer (eTimerPrec aPrecision)
00011 : tic( 0.0 ),
00012 ticStart( 0 ),
00013 ticLast( 0 )
00014 {
00015 Start();
00016 }
00017
00018 gTimer::~gTimer ()
00019 {
00020 }
00021
00022 double gTimer::GetSecondsFromTics (t_gTicElapsed aElapsed)
00023 {
00024 double q, divider;
00025
00026 divider = (double)CLOCKS_PER_SEC;
00027 ASSERTION(divider>0,"divider>0");
00028 q = (double)aElapsed;
00029 return q / divider;
00030 }
00031
00032 void gTimer::Start ()
00033 {
00034 ;
00035 ticStart = (t_gTicElapsed)clock();
00036 tic = (double)ticStart;
00037 }
00038
00039 gTimerTic::gTimerTic ()
00040 : gTimer( gTimer::e_PrecTic ),
00041 ticDiff( 0 )
00042 {
00043 thisGetTime( timeStart );
00044 }
00045
00046 gTimerTic::~gTimerTic ()
00047 {
00048 }
00049
00050 t_gTicElapsed gTimerTic::CpuTics ()
00051 {
00052 clock_t uClock = clock();
00053 ASSERTION(uClock!=(clock_t)-1,"uClock!=-1");
00054 ticLast = (t_gTicElapsed)uClock;
00055 if ( ticLast < ticStart ) {
00056 ticLast += CLOCKS_PER_SEC;
00057 }
00058 ASSERTION(ticLast>=ticStart,"ticLast>=ticStart");
00059 ticDiff = ticLast - ticStart;
00060 tic = (double)ticDiff;
00061 ticStart = ticLast;
00062 GetTimeNow( timeEnd );
00063 return ticDiff;
00064 }
00065
00066 double gTimerTic::GetElapsedMilisec ()
00067 {
00068 return GetMilisecsDiff( timeEnd, timeStart );
00069 }
00070
00071 t_uint32 gTimerTic::GetMilisec ()
00072 {
00073
00074 return (t_uint32)( ceil( GetElapsedMilisec() ) );
00075 }
00076
00077 void gTimerTic::Reset ()
00078 {
00079 ticDiff = ticLast = 0;
00080 ticStart = (t_gTicElapsed)clock();
00081 timeStart.Copy( timeEnd );
00082 }
00083
00084 double gTimerTic::GetMilisecsDiff (sTimeSeconds& aTime1, sTimeSeconds& aTime0)
00085 {
00086 double total1 = aTime1.GetMiliSecs();
00087 double total0 = aTime0.GetMiliSecs();
00088
00089 if ( total1>=total0 ) return total1-total0;
00090 return 0.0;
00091 }
00092
00093 int gTimerTic::thisGetTime (sTimeSeconds& aTime)
00094 {
00095 static struct timeval aTimeVal;
00096 int result;
00097 aTime.ToDefault();
00098 #ifdef linux
00099 result = gettimeofday( &aTimeVal, NULL );
00100 #else
00101 result = 0;
00102 #endif //linux
00103 if ( result!=0 ) return result;
00104 aTime.sec = aTimeVal.tv_sec;
00105 aTime.microSec = aTimeVal.tv_usec;
00106 DBGPRINT_MIN("DBG: sec.microSec: %lu.%06lu (result=%d)\n",
00107 (unsigned long)aTime.sec, (unsigned long)aTime.microSec,
00108 result);
00109 return 0;
00110 }
00111
00112