// Name: Timers.h // // Description: // // // Version Date Author Comment // ----------- ----------- ----------- ----------- // 00.00.01 08/15/97 king Creation #ifndef _TIMERS_CLASS_LIBRARY #define _TIMERS_CLASS_LIBRARY #include #include #include const int MaxStringSize = 1024; // Name: CTimer // // Description: // // Public functions defined for CTimer: // // double ElapsedTime(EPrecisionType ePrecision); // char* Report(EPrecisionType ePrecision); // void Reset(void); // virtual void Start(void) = 0; // virtual void Stop(void) = 0; // // // Version Date Author Comment // ----------- ----------- ----------- ----------- // 00.00.01 08/15/97 king Creation class CTimer { public: enum EPrecisionType { MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, AUTO }; CTimer( ) { m_dReferenceTime = 0.0; m_dTotalTime = 0.0; } // Name: Report // // Description: // // // Version Date Author Comment // ----------- ----------- ----------- ----------- // 00.00.01 08/15/97 king Creation const char* Report(EPrecisionType ePrecision = AUTO) { double dValue = m_dTotalTime; char* pUnits; switch (int(ePrecision)) { case HOURS: dValue /= CLOCKS_PER_SEC * 3600.0; pUnits = "hours"; break; case MINUTES: dValue /= CLOCKS_PER_SEC * 60.0; pUnits = "minutes"; break; case SECONDS: dValue /= CLOCKS_PER_SEC; pUnits = "seconds"; break; case MILLISECONDS: dValue /= CLOCKS_PER_SEC / 1000.0; pUnits = "milliseconds"; break; case AUTO: return ReportAutoTime(dValue / CLOCKS_PER_SEC); } static char szBuffer[MaxStringSize]; sprintf(szBuffer, "%g %s", dValue, pUnits); return (const char *) szBuffer; } void Reset(void) { m_dTotalTime = 0.0;} virtual void Start(void) = 0; virtual void Stop(void) = 0; protected: double m_dReferenceTime; double m_dTotalTime; private: // Name: ReportAutoTime // // Description: // // // Version Date Author Comment // ----------- ----------- ----------- ----------- // 00.00.01 08/15/97 king Creation const char* ReportAutoTime(double dSeconds) { static char szBuffer[MaxStringSize]; if (dSeconds < 1.0) sprintf(szBuffer, "%g milliseconds", dSeconds * 1000.0); else if (dSeconds < 60.0) sprintf(szBuffer, "%g seconds", dSeconds); else if (dSeconds < 3600.0) sprintf(szBuffer, "%g minutes", dSeconds / 60.0); else sprintf(szBuffer, "%g hours", dSeconds / 3600.0); return (const char *) szBuffer; } }; // Name: CCpuTimer // // Description: // // // Version Date Author Comment // ----------- ----------- ----------- ----------- // 00.00.01 08/15/97 king Creation class CCpuTimer : public CTimer { public: void Start(void) { clock_t sTimeValue; sTimeValue = clock( ); m_dReferenceTime = double(sTimeValue); } void Stop(void) { clock_t sTimeValue; sTimeValue = clock( ); m_dTotalTime += double(sTimeValue) - m_dReferenceTime; } private: }; #endif // _TIMERS_CLASS_LIBRARY